文档章节

ChartDirector创建彩色甘特图

咲晚杍
 咲晚杍
发布于 2013/09/24 13:50
字数 1051
阅读 396
收藏 1

甘特图非常适合用于展示工作进度,而如果把甘特图的进度条以不同的颜色进行区分,将会进一步提高甘特图的可读性。本示例演示用Web图表控件ChartDirector创建彩色的甘特图,这个甘特图中有3个Milestone标签。如图所示:

 

甘特图的彩色进度条来自一个彩色箱须盒,用到了XYChart.addBoxWhiskerLayer2参数。而Milestone标签来自散点层,用到了XYChart.addScatterLayer参数。在本例中,数据序列用于指定彩色条的任务号,这样可以把同一个任务的彩色条连接起来。任务号作为彩色条的X轴坐标,用 Layer.setXData 参数进行设置。另一个数据序列用于指定甘特图进度条的颜色。

ChartDirector创建彩色甘特图示例代码:

#include "chartdir.h"

int main(int argc, char *argv[])
{
    // The tasks for the gantt chart
    const char *labels[] = {"Market Research", "Define Specifications",
        "Overall Archiecture", "Project Planning", "Detail Design",
        "Software Development", "Test Plan", "Testing and QA", "User Documentation"};

    // The task index, start date, end date and color for each bar
    double taskNo[] = {0, 0, 1, 2, 3, 4, 5, 6, 6, 7, 8, 8};
    double startDate[] = {Chart::chartTime(2004, 8, 16), Chart::chartTime(2004, 10, 4
        ), Chart::chartTime(2004, 8, 30), Chart::chartTime(2004, 9, 13),
        Chart::chartTime(2004, 9, 20), Chart::chartTime(2004, 9, 27),
        Chart::chartTime(2004, 10, 4), Chart::chartTime(2004, 10, 4),
        Chart::chartTime(2004, 10, 25), Chart::chartTime(2004, 11, 1),
        Chart::chartTime(2004, 10, 18), Chart::chartTime(2004, 11, 8)};
    double endDate[] = {Chart::chartTime(2004, 8, 30), Chart::chartTime(2004, 10, 18
        ), Chart::chartTime(2004, 9, 13), Chart::chartTime(2004, 9, 27),
        Chart::chartTime(2004, 10, 4), Chart::chartTime(2004, 10, 11),
        Chart::chartTime(2004, 11, 8), Chart::chartTime(2004, 10, 18),
        Chart::chartTime(2004, 11, 8), Chart::chartTime(2004, 11, 22),
        Chart::chartTime(2004, 11, 1), Chart::chartTime(2004, 11, 22)};
    int colors[] = {0x00cc00, 0x00cc00, 0x00cc00, 0x0000cc, 0x0000cc, 0xcc0000,
        0xcc0000, 0x0000cc, 0xcc0000, 0xcc0000, 0x00cc00, 0xcc0000};

    // Create a XYChart object of size 620 x 325 pixels. Set background color to
    // light red (0xffcccc), with 1 pixel 3D border effect.
    XYChart *c = new XYChart(620, 325, 0xffcccc, 0x000000, 1);

    // Add a title to the chart using 15 points Times Bold Itatic font, with white
    // (ffffff) text on a dark red (800000) background
    c->addTitle("Mutli-Color Gantt Chart Demo", "timesbi.ttf", 15, 0xffffff
        )->setBackground(0x800000);

    // Set the plotarea at (140, 55) and of size 460 x 200 pixels. Use alternative
    // white/grey background. Enable both horizontal and vertical grids by setting
    // their colors to grey (c0c0c0). Set vertical major grid (represents month
    // boundaries) 2 pixels in width
    c->setPlotArea(140, 55, 460, 200, 0xffffff, 0xeeeeee, Chart::LineColor, 0xc0c0c0,
        0xc0c0c0)->setGridWidth(2, 1, 1, 1);

    // swap the x and y axes to create a horziontal box-whisker chart
    c->swapXY();

    // Set the y-axis scale to be date scale from Aug 16, 2004 to Nov 22, 2004, with
    // ticks every 7 days (1 week)
    c->yAxis()->setDateScale(Chart::chartTime(2004, 8, 16), Chart::chartTime(2004,
        11, 22), 86400 * 7);

    // Set multi-style axis label formatting. Month labels are in Arial Bold font in
    // "mmm d" format. Weekly labels just show the day of month and use minor tick
    // (by using '-' as first character of format string).
    c->yAxis()->setMultiFormat(Chart::StartOfMonthFilter(),
        "<*font=arialbd.ttf*>{value|mmm d}", Chart::StartOfDayFilter(), "-{value|d}")
        ;

    // Set the y-axis to shown on the top (right + swapXY = top)
    c->setYAxisOnRight();

    // Set the labels on the x axis
    c->xAxis()->setLabels(StringArray(labels, (int)(sizeof(labels) / sizeof(labels[0]
        ))));

    // Reverse the x-axis scale so that it points downwards.
    c->xAxis()->setReverse();

    // Set the horizontal ticks and grid lines to be between the bars
    c->xAxis()->setTickOffset(0.5);

    // Add some symbols to the chart to represent milestones. The symbols are added
    // using scatter layers. We need to specify the task index, date, name, symbol
    // shape, size and color.
    double coor1[] = {1};
    double date1[] = {Chart::chartTime(2004, 9, 13)};
    c->addScatterLayer(DoubleArray(coor1, (int)(sizeof(coor1) / sizeof(coor1[0]))),
        DoubleArray(date1, (int)(sizeof(date1) / sizeof(date1[0]))), "Milestone 1",
        Chart::Cross2Shape(), 13, 0xffff00);
    double coor2[] = {3};
    double date2[] = {Chart::chartTime(2004, 10, 4)};
    c->addScatterLayer(DoubleArray(coor2, (int)(sizeof(coor2) / sizeof(coor2[0]))),
        DoubleArray(date2, (int)(sizeof(date2) / sizeof(date2[0]))), "Milestone 2",
        Chart::StarShape(5), 15, 0xff00ff);
    double coor3[] = {5};
    double date3[] = {Chart::chartTime(2004, 11, 8)};
    c->addScatterLayer(DoubleArray(coor3, (int)(sizeof(coor3) / sizeof(coor3[0]))),
        DoubleArray(date3, (int)(sizeof(date3) / sizeof(date3[0]))), "Milestone 3",
        Chart::TriangleSymbol, 13, 0xff9933);

    // Add a multi-color box-whisker layer to represent the gantt bars
    BoxWhiskerLayer *layer = c->addBoxWhiskerLayer2(DoubleArray(startDate, (int)(
        sizeof(startDate) / sizeof(startDate[0]))), DoubleArray(endDate, (int)(
        sizeof(endDate) / sizeof(endDate[0]))), DoubleArray(), DoubleArray(),
        DoubleArray(), IntArray(colors, (int)(sizeof(colors) / sizeof(colors[0]))));
    layer->setXData(DoubleArray(taskNo, (int)(sizeof(taskNo) / sizeof(taskNo[0]))));
    layer->setBorderColor(Chart::SameAsMainColor);

    // Divide the plot area height ( = 200 in this chart) by the number of tasks to
    // get the height of each slot. Use 80% of that as the bar height.
    layer->setDataWidth(200 * 4 / 5 / (int)(sizeof(labels) / sizeof(labels[0])));

    // Add a legend box at (140, 265) - bottom of the plot area. Use 8 pts Arial Bold
    // as the font with auto-grid layout. Set the width to the same width as the plot
    // area. Set the backgorund to grey (dddddd).
    LegendBox *legendBox = c->addLegend2(140, 265, Chart::AutoGrid, "arialbd.ttf", 8)
        ;
    legendBox->setWidth(461);
    legendBox->setBackground(0xdddddd);

    // The keys for the scatter layers (milestone symbols) will automatically be
    // added to the legend box. We just need to add keys to show the meanings of the
    // bar colors.
    legendBox->addKey("Market Team", 0x00cc00);
    legendBox->addKey("Planning Team", 0x0000cc);
    legendBox->addKey("Development Team", 0xcc0000);

    // Output the chart
    c->makeChart("colorgantt.png");

    //free up resources
    delete c;
    return 0;
}

本文转载自:http://www.evget.com/zh-CN/Info/catalog/18674.html

咲晚杍
粉丝 7
博文 75
码字总数 8143
作品 0
渝中
私信 提问
Web图表开发工具JFreeChart与ChartDirector使用评测

在Web开发中,图形和图表是常用的数据呈现方式,图表能将枯燥的数据生动的表现出来。也许你正接手一个Web图表开发项目,却被Web图表开发工具选型的问题困扰着,要么是价格昂贵,要么就是部署...

咲晚杍
2013/08/21
768
0
Qt之自绘制饼图和绘图插件比较

最近在搞绘图方面的工作,说实话C++的第三方绘图库并不算多,总之我了解的有:qtcharts、ChartDirector、qwt、kdchart和QCustomPlot。这几个库各有利弊。 qtcharts:qt5.7之后才开源的模块,...

wangxujun59
10/08
22
0
ChartDirector破解及中文问题解决

ChartDir是一个比Jfreechart还要好的图表工具包,但要收费. ChartDir Project home: http://www.advsofteng.com/ 下一个来试验吧 http://download2.advsofteng.com/chartdir_java.war ------......

红薯
2009/03/06
13.7K
1
ChartDirector python版安装记录

ChartDirector 是一款商业的图表库,有多种语言的版本,使用它做的图表非常的精细漂亮,提供免费版本,但会出现logo信息。网上有很多关于它的破解方法。 下面是ChartDirector python版安装记...

绿悠悠
2010/06/19
1K
2
ChartDirector resize 怎么整?求大神帮忙!!

ChartDirector resize 怎么整?求大神帮忙!!

lcl
2013/03/11
240
0

没有更多内容

加载失败,请刷新页面

加载更多

【TencentOS tiny】深度源码分析(4)——消息队列

消息队列 在前一篇文章中【TencentOS tiny学习】源码分析(3)——队列 我们描述了TencentOS tiny的队列实现,同时也点出了TencentOS tiny的队列是依赖于消息队列的,那么我们今天来看看消息...

杰杰1号
22分钟前
6
0
Hive

这就是那个 JAVA 类 package cn.itcast.bigdata;import java.util.HashMap;import org.apache.hadoop.hive.ql.exec.UDF;public class PhoneNbrToArea extends UDF{privat......

Garphy
22分钟前
6
0
Springboot开发,第二天

SpringBoot学习,第二天 目录:1、Springboot整合Listener 2、Springboot访问静态资源 3、异常处理 4、热部署 一、SpringBoot整合Listener 两种方式完成组件的注册 1、通过注解扫描完成组件的...

有一个小阿飞
26分钟前
7
0
BeginnersBook Perl 教程

来源:ApacheCN BeginnersBook 翻译项目 译者:飞龙 协议:CC BY-NC-SA 4.0 贡献指南 本项目需要校对,欢迎大家提交 Pull Request。 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并...

ApacheCN_飞龙
38分钟前
5
0
我的Java秋招面经大合集

阿里面经 阿里中间件研发面经 蚂蚁金服研发面经 岗位是研发工程师,直接找蚂蚁金服的大佬进行内推。 我参与了阿里巴巴中间件部门的提前批面试,一共经历了四次面试,拿到了口头offer。 然后我...

Java技术江湖
43分钟前
8
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部