| 上位机MFC扩展编程图表自定义图例位置与外观 
 在创建一个图表时,其默认图例显示位置共有五个,分别是上,下,左,右,右上。
 对应枚举类型为:
 enum LegendPosition
 {
 LP_NONE,
 LP_TOP,
 LP_BOTTOM,
 LP_LEFT,
 LP_RIGHT,
 LP_TOPRIGHT
 };
 
 可通过图表控件进行设置:
 SetLegendPosition(BCGPChartLayout:
  P_NONE); LP_NONE表示不显示图例。
 默认只有五个位置,如果还想自定义图例显示位置的话,
 就得自己从CBCGPChartVisualObject派生类来实现。
 
 另外如果图例默认外观不满意也是可以在派生类中进行重绘。
 当前例程实现从CBCGPChartVisualObject派生自己的类CMyChart,
 实现自定义图例外观的绘制与图例左上角显示的新位置.
 例程界面如下:
 
   点击自绘图例外观,可以显示派生类中绘制的图例外观。
 多次点击更改图例位置,会依次循环更改图例的位置:
 不显示,上,下,左,右,右上角,左上角(自定义).
 自定义类源代码为:
 
 重绘图例按钮点击函数为:复制代码class CMyChart : public CBCGPChartVisualObject
{
        DECLARE_DYNCREATE(CMyChart)
protected:
        CBCGPBrush        m_brWhite;
        CBCGPPoint        m_ptLegendCustomPos;
        BOOL m_bOwnerDrawLegendKey;
public:
        //图例是否在自定义位置;
        BOOL IsLegendCustomPosition() const
        {
                return m_ptLegendCustomPos != CBCGPPoint(-1, -1);
        }
        //图例是水平还是垂直布局;
        virtual BOOL IsLegendHorizontal() const 
        {
                if (IsLegendCustomPosition())
                        return FALSE;//图例在自定义位置表示垂直;
                return CBCGPChartVisualObject::IsLegendHorizontal();
        }
        //图例自绘开,开;
        void EnableLegendCustomDraw(BOOL bEnable = true)
        {
                EnableDrawLegendShape(bEnable);
                m_bOwnerDrawLegendKey = bEnable;
        }
        //图例自定义位置开、关;
        void EnableLegendCustomPosition(BOOL bEnable, BOOL bLegendOverlapsChart = FALSE)
        {
                m_ptLegendCustomPos = bEnable ? CBCGPPoint(0, 25) : CBCGPPoint(-1, -1);
                m_chartLayout.m_bLegendOverlapsChart = bLegendOverlapsChart;
                m_rectPlotAreaPadding.left = 0;
        }
protected:
        CMyChart()
        {
                m_bOwnerDrawLegendKey = FALSE;
                m_brWhite.SetColor(CBCGPColor::White, 0.7);
                m_ptLegendCustomPos.SetPoint(-1, -1);
        }
        //由库调用,计算数据标签大小;
        virtual BOOL OnCalcDataLabelSize(CBCGPGraphicsManager* pGM, const CString& strText, CBCGPChartSeries* pSeries, 
 更改图例显示位置按钮函数为:复制代码void CMy123View::OnBnClickedCheck1()
{
        BOOL bOwnerDraw = ((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck();
        //获取自定义图表;
        CMyChart* pChart = DYNAMIC_DOWNCAST(CMyChart,m_wndChart.GetChart() );
        ASSERT_VALID(pChart);
        pChart->EnableLegendCustomDraw(bOwnerDraw);
        pChart->Redraw();
}
 在使用图例前,得创建一个图表,添加一些数据来演示。复制代码void CMy123View::OnBnClickedButton2()
{
        static int nLegendPosition=0;//维护图表位置;
        BOOL bOwnerDrawPos = ((CButton*)GetDlgItem(IDC_CHECK2))->GetCheck();
        if(bOwnerDrawPos)
                nLegendPosition +=1;
        //获取自定义图表;
        CMyChart* pChart = DYNAMIC_DOWNCAST(CMyChart,m_wndChart.GetChart() );
        ASSERT_VALID(pChart);
        //设置图例位置;
        if(nLegendPosition>6)
                nLegendPosition=0;
        if(nLegendPosition==6)
        {
                pChart->EnableLegendCustomPosition(true);//自定义图例位置于图表左上角;
        }
        else
        {
                pChart->EnableLegendCustomPosition(false);
                pChart->SetLegendPosition((BCGPChartLayout::LegendPosition)nLegendPosition);
        }
        pChart->SetDirty(true,true);
}
例程中图表创建函数为:
 
 例程下载地址:复制代码BOOL CMy123View::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, 
 
   如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!    
 
 
 
 |