CBarChart柱形图类

在用VC做有关图表的时候,感觉不是那么方便,在codeproject找到一个柱形图的实用类,原文地址为:http://www.codeproject.com/KB/miscctrl/CBarChart.aspx。它可以很快速的生成柱状图,并且支持输出位图、支持打印、支持从数据库导入。

下面介绍下基本步骤:

1.添加BarChart.cpp 和 BarChart.h工程;

2.在需要使用的地方添加头文件

#include "BarChart.h"

3.定义一个CBarChart的变量;

CBarChart    m_chart;

4.在实现文件里面使用Create方法创建图表柱形图;

      1).创建图表

// Create a bar chart control
    if (!m_chart.Create(
        rcBound,              // Bounding rectangle of the control
        this,                 // A pointer to the parent window to be shown on
        0 ))                  // ID of this control
    {
        // Error, can‘t create the chart
    }
    //If Create suceeds, it returns TRUE.

      2).设置标题、背景颜色

m_chart.SetTitle("A test chart, displaying some dummy data...");
m_chart.SetBKColor(RGB(255, 255, 240));

      3).添加柱形图表

m_chart.AddBar(
1600.356,           // A value to be shown at the top of the bar
"Jan",              // A label under the bar chart
RGB(255,255,0));    // Color of the bar

      4).网格线显示、柱形描述、柱形提示

m_chart.ShowGrid(m_bShowGrid, TRUE);            // Show/Hide grids
m_chart.ShowBarText(1, m_bShowText, TRUE);      // Show/Hide values(top)
m_chart.ShowLabel(m_bShowLabel, TRUE);          // Show/Hide labels(bottom)
m_chart.ShowTooltip(m_bShowTip);                // Activate/deactivate tooltips

      5).网格线大小

SetGridLines(
int nHorLineCount,      // Number of horizontal lines, 0 = ignore
int nVerLineCount,      // Number of vertical lines, 0 = ignore
BOOL bFixedSize,        // If TRUE, first 2 parameters will be ignored
int nFixedSize)         // A fixed value that defines distance between 2 lines,
                        // Will be used if previous parameter is set to TRUE

    6).缩放

m_chart.SetAutoScale(m_bScale);         // Set chart to be auto scaled
// Reposition window, so it finds out the size to fit into
m_chart.SetWindowPos( 0, 0, 0,
rcClient.Width(),
rcClient.Height() , SWP_NOMOVE);
m_chart.Refresh();                      // Force the chart to redraw itself

// You might also need to override OnSize of the
// Parent to position the chart, so it is always at the size you like

      7).删除柱形

m_chart.RemoveAt(nRemIndex);    // Removes the item indexed nRemIndex.
                                // Index is zero based.
m_chart.RemoveAll();            // Removes all bars

      8).保存bmp文件

// Save chart as a bitmap file
if (!m_chart.SaveToFile())
    {
    AfxMessageBox(m_chart.GetLastErrorMessage());
    };

      9).打印

m_chart.Print();        // Prints the whole chart fitted in the page.
// If you have a lot of bars, I recommend selecting landscape in the
// print dialog box.

      10).连接数据库

//--------------------------------------------创建ODBC连接
// Use this form to call a stored procedure or a query and use
// result set as chart input

ReadFromDatabase("DS Name", "Stored proc/Query Name", "List of all parameters",
         "Bars Color", "UserName", "Password"));

// Note that the query or stored procedure MUST have at least 2 columns,
// First column MUST be of type char with maximum length of 50 and
// Second a double. These columns will be used as input data for the chart.

//--------------------------------------------连接数据库表
m_chart.ReadFromDatabase("DS Name", "Table name", "Filter",
"Laabel column name", "dValue column name",
Bars color , "username", "password");

//--------------------------------------------显示
if (!m_chart.ReadFromDatabase("CHTst", "SpChartGetSale", "1, 12",
                      RGB(0, 0, 0), "hamed", "hamed"))
    {
        AfxMessageBox(m_chart.GetLastDatabaseErrMessage());
    };
or
m_chart.ReadFromDatabase("CHTst", "Sales", "",
"Month", "SaleRate",
RGB(0, 0, 0) , "hamed", "hamed");

 

5.实例

CRect rcClient;
    GetClientRect(&rcClient);

    if (!m_chart.Create(CRect(110, 50,
            rcClient.Width()-10,
            rcClient.Height() - 50), this, 1050 ))
    {
        if (!m_chart.GetSafeHwnd())
        {
            AfxMessageBox("Unable to create the chart control");
            return;
        }

        m_chart.Reset();
        m_chart.SetAutoScale(FALSE);
    }

    m_chart.SetTitle("A test chart, displaying some dummy data...");
    m_chart.SetBKColor(RGB(255, 255, 240));
    m_chart.ShowTooltip(TRUE);

    m_chart.AddBar(1600.356,"Jan", RGB(255,255,0));
    m_chart.AddBar(2841.5468,"Feb", RGB(255,0,0));
    m_chart.AddBar(1045.3258,"Mar", RGB(100,100,200));
    m_chart.AddBar(1502.215,"Apr", RGB(0,255,0));
    m_chart.AddBar(1467,"MAY", RGB(255,255,255));
    m_chart.AddBar(1678.354,"JUN", RGB(200,255,255));
    m_chart.AddBar(1785.689,"JUL", RGB(255,240,40));
    m_chart.AddBar(1283.099,"AUG", RGB(255,60,130));
    m_chart.AddBar(1554.879,"SEP", RGB(255,255,200));
    m_chart.AddBar(1400.10,"OCT", RGB(130,235,250));
    m_chart.AddBar(1600.556,"NOV", RGB(100,150,200));
    m_chart.AddBar(1900.3546,"DES", RGB(150,240,80), TRUE);

运行结果:

时间: 2024-08-04 23:39:47

CBarChart柱形图类的相关文章

***百度统计图表Echarts的php实现类,支持柱形图、线形图、饼形图

/** * 百度数据统计图表echart的PHP实现类 * * 原作者: * @author: chenliujin <[email protected]> * @since 2013-12-12 * * 修改者: * @author: iamlintao <http://www.iamlintao.com> * @since: 2014-06-25 * @version: * @revision: * * 修改后支持 柱形图(bar).线形图(line).饼形图(pie) * *

JFreeChart之堆叠柱形图(StackedBar)

JFreeChart之堆叠柱形图(StackedBar) JAVA JFreeChart 最近的项目使用有个功能需要使用到堆叠柱形图,看了项目以前的代码实现没有想要的结果.所以自己就先到官网下载了 Demo,Demo里有个打包好的Jar包,直接运行看到效果,但是坑爹的是貌似没有对应的源码,所以只能根据class名称直接google了,所幸在github里找到对应的源码. 点我下载 访问密码 f62b 这是运行Demo找到想要实现的效果的大致图: 我最终想要实现的效果是这样的: 如果想要实现这个效

【Qt5开发及实例】24、数据柱形图显示

数据柱形图显示 1.我们首先把这个这个视图的表格部分表示出来 mainwindow.h /** * 书本:[Qt5开发及实例] * 功能:数据柱形图显示,这个类是表格显示 * 文件:mainwindow.h * 时间:2015年1月28日18:50:54 * 作者:cutter_point */ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QStandardItemModel>

AChartEngine 安卓折线图 柱形图等利器

http://www.eoeandroid.com/thread-548233-1-6.html 最近公司项目中要用到折线图,状态类型的图标要用到折线图,柱形图等,并且能够动态显示,在网上找了许多demo,基本上都是写死的数据,下面先让我们看看AChartEngine的介绍 AChartEngine 运行示例图 : 每个图表都需要一个数据集 (Dataset) 和 渲染器集合 (Renderer); -- 数据集 : 又由许多数据组成, -- 渲染器 : 也由不同的子渲染器组成, -- 获取Ac

XCL-Charts图表库中柱形图的同源风格切换介绍

柱形图是被使用最多的图之中的一个,在写XCL-Charts这个Android图表库时,为它花费的时间相当多,不是由于有多难绘制,而是要在设计时怎样才干保证图基类能适应各种情况,能灵活满足足够多的需求,以及够简洁的接口,并要开放出足够多的绘制元素和參数供开发者定制,同一时候对各类柱形图独有的的特点要加以突出,再加上柱形图的选项本身相对于其他图来说也是相当多的,所以花了比較多的时间.结果嘛,至少我自己临时认为还算不错. 这里我仅仅简单给大家介绍XCL-Charts图表库中柱图一个比較有意思的特点:

High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件

原文地址:https://www.codeproject.com/articles/14075/high-speed-charting-control 本文翻译在CodeProject上的介绍(主要还是谷歌翻译,看不太明白的地方,请对比原文,敬请原谅),方便自己和后面人的学习(花费了两天时间,希望是值得的).推荐一个前辈写的东西:TeeChart替代品,MFC下好用的高速绘图控件-(Hight-Speed Charting),自己也转载了这篇文章,在转载的文章中根据自己的实验修改了一些东西,修改

Android自定义view之绘制实用型的柱形图和折线图

概述: 前几天突然需要做两种图表--柱形图.折线图,于是第一反应是先看看网上有没有现成的,结果有是有,但都不是我想要的,而且大多数不是用纯android代码完成,HTML5似乎完成这类工作要容易得多,但是我本人并不会HTML5,只能黯然神伤,掩面流泪,最终只能自己敲代码了. **知识点:**android自定义view.图形图像.Fragment.MVC模式. Demo 界面是模仿红圈营销搭建的 折线图: 代码,注释很详细,直接看代码就行了: public class LineChartView

Android自定义控件系列四:绘制实用型的柱形图和折线图

概述: 前几天突然需要做两种图表--柱形图.折线图,于是第一反应是先看看网上有没有现成的,结果有是有,但都不是想要的,而且大多数不是用纯android代码完成,不过HTML5似乎完成这类工作要容易得多,单是非我们所擅长. **知识点:**android自定义view.图形图像.Fragment.MVC模式. Demo 界面是模仿红圈营销搭建的 折线图:  代码,注释很详细,直接看代码就行了: 001.public class LineChartView extends View { 002. 0

XCL-Chart柱形图的期望线/分界线

周日在柱形图上加了两个小功能,其中之一是加上了期望线/分界线,功能很小,但我个人很喜欢这个功能(好像之前也没看到别的图表库原生支持这个.) 主要是加上这些小小的横线后,能很明显的区分出数据的层次.通过柱形与线的对比,可以一下就知道,目前处于什么层次或阶级. 这种功能在强调某个底线或分级时特别有感觉.比如,销售人员的销售底线,价格的红线等,在商业报表中表强调时应当很有用,不过我在Demo中是举了个考试成绩的柱形图.虽不那么商业化,但应当可以比较让人好理解这个线的意义所在. 图如下: 附上XCL-C