LCOV - code coverage report
Current view: top level - chart2/source/view/charttypes - BubbleChart.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 140 200 70.0 %
Date: 2015-06-13 12:38:46 Functions: 11 14 78.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "BubbleChart.hxx"
      21             : #include "PlottingPositionHelper.hxx"
      22             : #include "AbstractShapeFactory.hxx"
      23             : #include "CommonConverters.hxx"
      24             : #include "macros.hxx"
      25             : #include "ViewDefines.hxx"
      26             : #include "ObjectIdentifier.hxx"
      27             : #include "Splines.hxx"
      28             : #include "LabelPositionHelper.hxx"
      29             : #include "Clipping.hxx"
      30             : #include "Stripe.hxx"
      31             : 
      32             : #include <com/sun/star/chart2/Symbol.hpp>
      33             : #include <com/sun/star/chart/DataLabelPlacement.hpp>
      34             : #include <editeng/unoprnms.hxx>
      35             : #include <rtl/math.hxx>
      36             : #include <com/sun/star/drawing/DoubleSequence.hpp>
      37             : #include <com/sun/star/drawing/NormalsKind.hpp>
      38             : #include <com/sun/star/lang/XServiceName.hpp>
      39             : 
      40             : namespace chart
      41             : {
      42             : using namespace ::com::sun::star;
      43             : using namespace ::rtl::math;
      44             : using namespace ::com::sun::star::chart2;
      45             : 
      46           3 : BubbleChart::BubbleChart( const uno::Reference<XChartType>& xChartTypeModel
      47             :                      , sal_Int32 nDimensionCount )
      48             :         : VSeriesPlotter( xChartTypeModel, nDimensionCount, false )
      49             :         , m_bShowNegativeValues(false)
      50             :         , m_bBubbleSizeAsArea(true)
      51             :         , m_fBubbleSizeScaling(1.0)
      52             :         , m_fMaxLogicBubbleSize( 0.0 )
      53           3 :         , m_fBubbleSizeFactorToScreen( 1.0 )
      54             : {
      55             :     // We only support 2 dimensional bubble charts
      56             :     assert(nDimensionCount == 2);
      57             : 
      58           3 :     if( !m_pMainPosHelper )
      59           3 :         m_pMainPosHelper = new PlottingPositionHelper();
      60           3 :     PlotterBase::m_pPosHelper = m_pMainPosHelper;
      61           3 : }
      62             : 
      63           9 : BubbleChart::~BubbleChart()
      64             : {
      65           3 :     delete m_pMainPosHelper;
      66           6 : }
      67             : 
      68           3 : void BubbleChart::calculateMaximumLogicBubbleSize()
      69             : {
      70           3 :     double fMaxSize = 0.0;
      71             : 
      72           3 :     sal_Int32 nStartIndex = 0;
      73           3 :     sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
      74           6 :     for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
      75             :     {
      76           3 :         ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator             aZSlotIter = m_aZSlots.begin();
      77           3 :         const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator  aZSlotEnd = m_aZSlots.end();
      78           6 :         for( ; aZSlotIter != aZSlotEnd; ++aZSlotIter )
      79             :         {
      80           3 :             ::std::vector< VDataSeriesGroup >::iterator             aXSlotIter = aZSlotIter->begin();
      81           3 :             const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
      82          12 :             for( ; aXSlotIter != aXSlotEnd; ++aXSlotIter )
      83             :             {
      84           9 :                 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
      85           9 :                 ::std::vector< VDataSeries* >::const_iterator       aSeriesIter = pSeriesList->begin();
      86           9 :                 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd  = pSeriesList->end();
      87          18 :                 for( ; aSeriesIter != aSeriesEnd; ++aSeriesIter )
      88             :                 {
      89           9 :                     VDataSeries* pSeries( *aSeriesIter );
      90           9 :                     if(!pSeries)
      91           0 :                         continue;
      92             : 
      93           9 :                     double fSize = pSeries->getBubble_Size( nIndex );
      94           9 :                     if( m_bShowNegativeValues )
      95           0 :                         fSize = fabs(fSize);
      96           9 :                     if( fSize > fMaxSize )
      97           6 :                         fMaxSize = fSize;
      98             :                 }
      99             :             }
     100             :         }
     101             :     }
     102             : 
     103           3 :     m_fMaxLogicBubbleSize = fMaxSize;
     104           3 : }
     105             : 
     106           3 : void BubbleChart::calculateBubbleSizeScalingFactor()
     107             : {
     108           3 :     double fLogicZ=1.0;
     109           3 :     drawing::Position3D aSceneMinPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMinX(),m_pMainPosHelper->getLogicMinY(),fLogicZ, false ) );
     110           3 :     drawing::Position3D aSceneMaxPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMaxX(),m_pMainPosHelper->getLogicMaxY(),fLogicZ, false ) );
     111             : 
     112           3 :     awt::Point aScreenMinPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMinPos ) );
     113           3 :     awt::Point aScreenMaxPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMaxPos ) );
     114             : 
     115           3 :     sal_Int32 nWidth = abs( aScreenMaxPos.X - aScreenMinPos.X );
     116           3 :     sal_Int32 nHeight = abs( aScreenMaxPos.Y - aScreenMinPos.Y );
     117             : 
     118           3 :     sal_Int32 nMinExtend = std::min( nWidth, nHeight );
     119           3 :     m_fBubbleSizeFactorToScreen = nMinExtend * 0.25;//max bubble size is 25 percent of diagram size
     120           3 : }
     121             : 
     122           3 : drawing::Direction3D BubbleChart::transformToScreenBubbleSize( double fLogicSize )
     123             : {
     124           3 :     drawing::Direction3D aRet(0,0,0);
     125             : 
     126           3 :     if( ::rtl::math::isNan(fLogicSize) || ::rtl::math::isInf(fLogicSize) )
     127           0 :         return aRet;
     128             : 
     129           3 :     if( m_bShowNegativeValues )
     130           0 :         fLogicSize = fabs(fLogicSize);
     131             : 
     132           3 :     double fMaxSize = m_fMaxLogicBubbleSize;
     133             : 
     134           3 :     double fMaxRadius = fMaxSize;
     135           3 :     double fRaduis = fLogicSize;
     136           3 :     if( m_bBubbleSizeAsArea )
     137             :     {
     138           3 :         fMaxRadius = sqrt( fMaxSize / F_PI );
     139           3 :         fRaduis = sqrt( fLogicSize / F_PI );
     140             :     }
     141             : 
     142           3 :     aRet.DirectionX = m_fBubbleSizeScaling * m_fBubbleSizeFactorToScreen * fRaduis / fMaxRadius;
     143           3 :     aRet.DirectionY = aRet.DirectionX;
     144             : 
     145           3 :     return aRet;
     146             : }
     147             : 
     148          12 : bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32 /*nDimensionIndex*/ )
     149             : {
     150          12 :     return true;
     151             : }
     152             : 
     153           0 : bool BubbleChart::isSeparateStackingForDifferentSigns( sal_Int32 /*nDimensionIndex*/ )
     154             : {
     155           0 :     return false;
     156             : }
     157             : 
     158          12 : LegendSymbolStyle BubbleChart::getLegendSymbolStyle()
     159             : {
     160          12 :     return LegendSymbolStyle_CIRCLE;
     161             : }
     162             : 
     163           3 : drawing::Direction3D BubbleChart::getPreferredDiagramAspectRatio() const
     164             : {
     165           3 :     return drawing::Direction3D(-1,-1,-1);
     166             : }
     167             : 
     168           9 : void BubbleChart::addSeries( VDataSeries* pSeries, sal_Int32 zSlot, sal_Int32 xSlot, sal_Int32 ySlot )
     169             : {
     170           9 :     VSeriesPlotter::addSeries( pSeries, zSlot, xSlot, ySlot );
     171           9 : }
     172             : 
     173             : //better performance for big data
     174             : struct FormerPoint
     175             : {
     176           0 :     FormerPoint( double fX, double fY, double fZ )
     177           0 :         : m_fX(fX), m_fY(fY), m_fZ(fZ)
     178           0 :         {}
     179           0 :     FormerPoint()
     180             :     {
     181           0 :         ::rtl::math::setNan( &m_fX );
     182           0 :         ::rtl::math::setNan( &m_fY );
     183           0 :         ::rtl::math::setNan( &m_fZ );
     184           0 :     }
     185             : 
     186             :     double m_fX;
     187             :     double m_fY;
     188             :     double m_fZ;
     189             : };
     190             : 
     191           3 : void BubbleChart::createShapes()
     192             : {
     193           3 :     if( m_aZSlots.empty() ) //no series
     194           0 :         return;
     195             : 
     196             :     OSL_ENSURE(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"BubbleChart is not proper initialized");
     197           3 :     if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
     198           0 :         return;
     199             : 
     200             :     //therefore create an own group for the texts and the error bars to move them to front
     201             :     //(because the text group is created after the series group the texts are displayed on top)
     202             :     uno::Reference< drawing::XShapes > xSeriesTarget(
     203           3 :         createGroupShape( m_xLogicTarget,OUString() ));
     204             :     uno::Reference< drawing::XShapes > xTextTarget(
     205           6 :         m_pShapeFactory->createGroup2D( m_xFinalTarget,OUString() ));
     206             : 
     207             :     //update/create information for current group
     208           3 :     double fLogicZ = 1.0;//as defined
     209             : 
     210           3 :     sal_Int32 nStartIndex = 0; // inclusive       ;..todo get somehow from x scale
     211           3 :     sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
     212           3 :     if(nEndIndex<=0)
     213           0 :         nEndIndex=1;
     214             : 
     215             :     //better performance for big data
     216           6 :     std::map< VDataSeries*, FormerPoint > aSeriesFormerPointMap;
     217           3 :     m_bPointsWereSkipped = false;
     218           3 :     sal_Int32 nSkippedPoints = 0;
     219           3 :     sal_Int32 nCreatedPoints = 0;
     220             : 
     221           3 :     calculateMaximumLogicBubbleSize();
     222           3 :     calculateBubbleSizeScalingFactor();
     223           3 :     if( m_fMaxLogicBubbleSize <= 0 || m_fBubbleSizeFactorToScreen <= 0 )
     224           0 :         return;
     225             : 
     226             :     //iterate through all x values per indices
     227           6 :     for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
     228             :     {
     229           3 :         ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator aZSlotIter = m_aZSlots.begin();
     230           3 :         const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator aZSlotEnd = m_aZSlots.end();
     231             : 
     232           6 :         for( sal_Int32 nZ=1; aZSlotIter != aZSlotEnd; ++aZSlotIter, nZ++ )
     233             :         {
     234           3 :             ::std::vector< VDataSeriesGroup >::iterator             aXSlotIter = aZSlotIter->begin();
     235           3 :             const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
     236             : 
     237          12 :             for( sal_Int32 nX=0; aXSlotIter != aXSlotEnd; ++aXSlotIter, ++nX )
     238             :             {
     239           9 :                 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
     240           9 :                 ::std::vector< VDataSeries* >::const_iterator       aSeriesIter = pSeriesList->begin();
     241           9 :                 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd  = pSeriesList->end();
     242             : 
     243             :                 //iterate through all series
     244          18 :                 for( sal_Int32 nSeriesIndex = 0; aSeriesIter != aSeriesEnd; ++aSeriesIter, ++nSeriesIndex )
     245             :                 {
     246           9 :                     VDataSeries* pSeries( *aSeriesIter );
     247           9 :                     if(!pSeries)
     248           6 :                         continue;
     249             : 
     250           9 :                     bool bHasFillColorMapping = pSeries->hasPropertyMapping("FillColor");
     251           9 :                     bool bHasBorderColorMapping = pSeries->hasPropertyMapping("LineColor");
     252             : 
     253           9 :                     uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes = getSeriesGroupShape(*aSeriesIter, xSeriesTarget);
     254             : 
     255           9 :                     sal_Int32 nAttachedAxisIndex = pSeries->getAttachedAxisIndex();
     256           9 :                     PlottingPositionHelper* pPosHelper = &(this->getPlottingPositionHelper( nAttachedAxisIndex ));
     257           9 :                     if(!pPosHelper)
     258           0 :                         pPosHelper = m_pMainPosHelper;
     259           9 :                     PlotterBase::m_pPosHelper = pPosHelper;
     260             : 
     261             :                     //collect data point information (logic coordinates, style ):
     262           9 :                     double fLogicX = pSeries->getXValue(nIndex);
     263           9 :                     double fLogicY = pSeries->getYValue(nIndex);
     264           9 :                     double fBubbleSize = pSeries->getBubble_Size( nIndex );
     265             : 
     266           9 :                     if( !m_bShowNegativeValues && fBubbleSize<0.0 )
     267           0 :                         continue;
     268             : 
     269           9 :                     if( ::rtl::math::approxEqual( fBubbleSize, 0.0 ) || ::rtl::math::isNan(fBubbleSize) )
     270           3 :                         continue;
     271             : 
     272          18 :                     if(    ::rtl::math::isNan(fLogicX) || ::rtl::math::isInf(fLogicX)
     273          12 :                         || ::rtl::math::isNan(fLogicY) || ::rtl::math::isInf(fLogicY) )
     274           3 :                         continue;
     275             : 
     276           3 :                     bool bIsVisible = pPosHelper->isLogicVisible( fLogicX, fLogicY, fLogicZ );
     277             : 
     278           3 :                     drawing::Position3D aUnscaledLogicPosition( fLogicX, fLogicY, fLogicZ );
     279           3 :                     drawing::Position3D aScaledLogicPosition(aUnscaledLogicPosition);
     280           3 :                     pPosHelper->doLogicScaling( aScaledLogicPosition );
     281             : 
     282             :                     //transformation 3) -> 4)
     283           3 :                     drawing::Position3D aScenePosition( pPosHelper->transformLogicToScene( fLogicX,fLogicY,fLogicZ, false ) );
     284             : 
     285             :                     //better performance for big data
     286           3 :                     FormerPoint aFormerPoint( aSeriesFormerPointMap[pSeries] );
     287           3 :                     pPosHelper->setCoordinateSystemResolution( m_aCoordinateSystemResolution );
     288           6 :                     if( !pSeries->isAttributedDataPoint(nIndex)
     289           6 :                             &&
     290             :                         pPosHelper->isSameForGivenResolution( aFormerPoint.m_fX, aFormerPoint.m_fY, aFormerPoint.m_fZ
     291           3 :                                                             , aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ ) )
     292             :                     {
     293           0 :                         nSkippedPoints++;
     294           0 :                         m_bPointsWereSkipped = true;
     295           0 :                         continue;
     296             :                     }
     297           3 :                     aSeriesFormerPointMap[pSeries] = FormerPoint(aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ);
     298             : 
     299             :                     //create a single datapoint if point is visible
     300           3 :                     if( !bIsVisible )
     301           0 :                         continue;
     302             : 
     303             :                     //create a group shape for this point and add to the series shape:
     304             :                     OUString aPointCID = ObjectIdentifier::createPointCID(
     305           6 :                         pSeries->getPointCID_Stub(), nIndex );
     306             :                     uno::Reference< drawing::XShapes > xPointGroupShape_Shapes(
     307           6 :                         createGroupShape(xSeriesGroupShape_Shapes,aPointCID) );
     308             :                     uno::Reference<drawing::XShape> xPointGroupShape_Shape =
     309           6 :                             uno::Reference<drawing::XShape>( xPointGroupShape_Shapes, uno::UNO_QUERY );
     310             : 
     311             :                     {
     312           3 :                         nCreatedPoints++;
     313             : 
     314             :                         //create data point
     315           3 :                         drawing::Direction3D aSymbolSize = transformToScreenBubbleSize( fBubbleSize );
     316           3 :                         uno::Reference<drawing::XShape> xShape;
     317           9 :                         xShape = m_pShapeFactory->createCircle2D( xPointGroupShape_Shapes
     318           6 :                                 , aScenePosition, aSymbolSize );
     319             : 
     320             :                         setMappedProperties( xShape
     321             :                                 , pSeries->getPropertiesOfPoint( nIndex )
     322           3 :                                 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
     323             : 
     324           3 :                         if(bHasFillColorMapping)
     325             :                         {
     326           0 :                             double nPropVal = pSeries->getValueByProperty(nIndex, "FillColor");
     327           0 :                             if(!rtl::math::isNan(nPropVal))
     328             :                             {
     329           0 :                                 uno::Reference< beans::XPropertySet > xProps( xShape, uno::UNO_QUERY_THROW );
     330           0 :                                 xProps->setPropertyValue("FillColor", uno::makeAny(static_cast<sal_Int32>(nPropVal)));
     331             :                             }
     332             :                         }
     333           3 :                         if(bHasBorderColorMapping)
     334             :                         {
     335           0 :                             double nPropVal = pSeries->getValueByProperty(nIndex, "LineColor");
     336           0 :                             if(!rtl::math::isNan(nPropVal))
     337             :                             {
     338           0 :                                 uno::Reference< beans::XPropertySet > xProps( xShape, uno::UNO_QUERY_THROW );
     339           0 :                                 xProps->setPropertyValue("LineColor", uno::makeAny(static_cast<sal_Int32>(nPropVal)));
     340             :                             }
     341             :                         }
     342             : 
     343           3 :                         ::chart::AbstractShapeFactory::setShapeName( xShape, "MarkHandles" );
     344             : 
     345             :                         //create data point label
     346           3 :                         if( (**aSeriesIter).getDataPointLabelIfLabel(nIndex) )
     347             :                         {
     348           0 :                             LabelAlignment eAlignment = LABEL_ALIGN_TOP;
     349             :                             drawing::Position3D aScenePosition3D( aScenePosition.PositionX
     350             :                                         , aScenePosition.PositionY
     351           0 :                                         , aScenePosition.PositionZ+this->getTransformedDepth() );
     352             : 
     353           0 :                             sal_Int32 nLabelPlacement = pSeries->getLabelPlacement( nIndex, m_xChartTypeModel, m_nDimension, pPosHelper->isSwapXAndY() );
     354             : 
     355           0 :                             switch(nLabelPlacement)
     356             :                             {
     357             :                             case ::com::sun::star::chart::DataLabelPlacement::TOP:
     358           0 :                                 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
     359           0 :                                 eAlignment = LABEL_ALIGN_TOP;
     360           0 :                                 break;
     361             :                             case ::com::sun::star::chart::DataLabelPlacement::BOTTOM:
     362           0 :                                 aScenePosition3D.PositionY += (aSymbolSize.DirectionY/2+1);
     363           0 :                                 eAlignment = LABEL_ALIGN_BOTTOM;
     364           0 :                                 break;
     365             :                             case ::com::sun::star::chart::DataLabelPlacement::LEFT:
     366           0 :                                 aScenePosition3D.PositionX -= (aSymbolSize.DirectionX/2+1);
     367           0 :                                 eAlignment = LABEL_ALIGN_LEFT;
     368           0 :                                 break;
     369             :                             case ::com::sun::star::chart::DataLabelPlacement::RIGHT:
     370           0 :                                 aScenePosition3D.PositionX += (aSymbolSize.DirectionX/2+1);
     371           0 :                                 eAlignment = LABEL_ALIGN_RIGHT;
     372           0 :                                 break;
     373             :                             case ::com::sun::star::chart::DataLabelPlacement::CENTER:
     374           0 :                                 eAlignment = LABEL_ALIGN_CENTER;
     375           0 :                                 break;
     376             :                             default:
     377             :                                 OSL_FAIL("this label alignment is not implemented yet");
     378           0 :                                 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
     379           0 :                                 eAlignment = LABEL_ALIGN_TOP;
     380           0 :                                 break;
     381             :                             }
     382             : 
     383             :                             awt::Point aScreenPosition2D( LabelPositionHelper(pPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory)
     384           0 :                                 .transformSceneToScreenPosition( aScenePosition3D ) );
     385           0 :                             sal_Int32 nOffset = 0;
     386           0 :                             if(LABEL_ALIGN_CENTER!=eAlignment)
     387           0 :                                 nOffset = 100;//add some spacing //@todo maybe get more intelligent values
     388           0 :                             createDataLabel( xTextTarget, **aSeriesIter, nIndex
     389           0 :                                             , fBubbleSize, fBubbleSize, aScreenPosition2D, eAlignment, nOffset );
     390           3 :                         }
     391             :                     }
     392             : 
     393             :                     //remove PointGroupShape if empty
     394           3 :                     if(!xPointGroupShape_Shapes->getCount())
     395           0 :                         xSeriesGroupShape_Shapes->remove(xPointGroupShape_Shape);
     396             : 
     397           3 :                 }//next series in x slot (next y slot)
     398             :             }//next x slot
     399             :         }//next z slot
     400             :     }//next category
     401             :     SAL_INFO(
     402             :         "chart2",
     403             :         "skipped points: " << nSkippedPoints << " created points: "
     404           3 :             << nCreatedPoints);
     405             : }
     406             : 
     407             : } //namespace chart
     408             : 
     409             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11