翻譯|使用教程|編輯:黃竹雯|2018-12-24 17:21:30.000|閱讀 420 次
概述:Web圖表控件ChartDirector連載教程分享之條形圖描影,內附下載和鏈接。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
ChartDirector是一個非常理想的圖表工具,它擁有廣泛的圖表類型、分層架構、實時互動的大數據表、普遍適應于各種應用程序以及支持PDF和SVG圖標等的優點。此系列連載旨在介紹ChartDirector的實用教程,供大家學習討論。

此示例演示如何使用條形漸變效果著色水平條形圖,以及使用漸變顏色背景、部分圓形框架和繪圖區域的大小自動調整來適應圖表。
以下代碼可在“cppdemo / gradientbar ”中找到。MFC版本的代碼可在“mfcdemo”中找到(僅限Windows版本)。QT版本的代碼可在“qtdemo”中找到。
#include "chartdir.h"
int main(int argc, char *argv[])
{
    // The data for the bar chart
    double data[] = {1350, 1600, 1950, 2300, 2700};
    // The labels for the bar chart
    const char *labels[] = {"2001", "2002", "2003", "2004", "2005"};
    // The colors for the bars
    int colors[] = {0xcc0000, 0x66aaee, 0xeebb22, 0xcccccc, 0xcc88ff};
    // Create a PieChart object of size 600 x 380 pixels.
    XYChart *c = new XYChart(600, 380);
    // Use the white on black palette, which means the default text and line colors are white
    c->setColors(Chart::whiteOnBlackPalette);
    // Use a vertical gradient color from blue (0000cc) to deep blue (000044) as background. Use
    // rounded corners of 30 pixels radius for the top-left and bottom-right corners.
    c->setBackground(c->linearGradientColor(0, 0, 0, c->getHeight(), 0x0000cc, 0x000044));
    c->setRoundedFrame(0xffffff, 30, 0, 30, 0);
    // Add a title using 18pt Times New Roman Bold Italic font. Add 6 pixels top and bottom margins
    // to the title.
    TextBox *title = c->addTitle("Annual Revenue for Star Tech", "timesbi.ttf", 18);
    title->setMargin(0, 0, 6, 6);
    // Add a separator line in white color just under the title
    c->addLine(20, title->getHeight(), c->getWidth() - 21, title->getHeight(), 0xffffff);
    // Tentatively set the plotarea at (70, 80) and of 480 x 240 pixels in size. Use transparent
    // border and white grid lines
    c->setPlotArea(70, 80, 480, 240, -1, -1, Chart::Transparent, 0xffffff);
    // Swap the axis so that the bars are drawn horizontally
    c->swapXY();
    // Add a multi-color bar chart layer using the supplied data. Use bar gradient lighting with the
    // light intensity from 0.75 to 2.0
    c->addBarLayer(DoubleArray(data, (int)(sizeof(data) / sizeof(data[0]))), IntArray(colors, (int)(
        sizeof(colors) / sizeof(colors[0]))))->setBorderColor(Chart::Transparent,
        Chart::barLighting(0.75, 2.0));
    // Set the labels on the x axis.
    c->xAxis()->setLabels(StringArray(labels, (int)(sizeof(labels) / sizeof(labels[0]))));
    // Show the same scale on the left and right y-axes
    c->syncYAxis();
    // Set the bottom y-axis title using 10pt Arial Bold font
    c->yAxis()->setTitle("USD (millions)", "arialbd.ttf", 10);
    // Set y-axes to transparent
    c->yAxis()->setColors(Chart::Transparent);
    c->yAxis2()->setColors(Chart::Transparent);
    // Disable ticks on the x-axis by setting the tick color to transparent
    c->xAxis()->setTickColor(Chart::Transparent);
    // Set the label styles of all axes to 8pt Arial Bold font
    c->xAxis()->setLabelStyle("arialbd.ttf", 8);
    c->yAxis()->setLabelStyle("arialbd.ttf", 8);
    c->yAxis2()->setLabelStyle("arialbd.ttf", 8);
    // Adjust the plot area size, such that the bounding box (inclusive of axes) is 30 pixels from
    // the left edge, 25 pixels below the title, 50 pixels from the right edge, and 25 pixels from
    // the bottom edge.
    c->packPlotArea(30, title->getHeight() + 25, c->getWidth() - 50, c->getHeight() - 25);
    // Output the chart
    c->makeChart("gradientbar.png");
    //free up resources
    delete c;
    return 0;
}

此示例演示了對條形圖使用柱面著色效果。以及展示了繪圖區域兩側的拉絲金屬背景、圓形框架和軸標簽。
以下代碼可在“cppdemo / cylinderlightbar ”中找到。MFC版本的代碼可在“mfcdemo”中找到(僅限Windows版本)。QT版本的代碼可在“qtdemo”中找到。
#include "chartdir.h"
int main(int argc, char *argv[])
{
    // The data for the bar chart
    double data[] = {450, 560, 630, 800, 1100, 1350, 1600, 1950, 2300, 2700};
    // The labels for the bar chart
    const char *labels[] = {"1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004",
        "2005"};
    // Create a XYChart object of size 600 x 380 pixels. Set background color to brushed silver,
    // with a 2 pixel 3D border. Use rounded corners of 20 pixels radius.
    XYChart *c = new XYChart(600, 380, Chart::brushedSilverColor(), Chart::Transparent, 2);
    // Add a title to the chart using 18pt Times Bold Italic font. Set top/bottom margins to 8
    // pixels.
    c->addTitle("Annual Revenue for Star Tech", "timesbi.ttf", 18)->setMargin(0, 0, 8, 8);
    // Set the plotarea at (70, 55) and of size 460 x 280 pixels. Use transparent border and black
    // grid lines. Use rounded frame with radius of 20 pixels.
    c->setPlotArea(70, 55, 460, 280, -1, -1, Chart::Transparent, 0x000000);
    c->setRoundedFrame(0xffffff, 20);
    // Add a multi-color bar chart layer using the supplied data. Set cylinder bar shape.
    c->addBarLayer(DoubleArray(data, (int)(sizeof(data) / sizeof(data[0]))), IntArray(0, 0)
        )->setBarShape(Chart::CircleShape);
    // Set the labels on the x axis.
    c->xAxis()->setLabels(StringArray(labels, (int)(sizeof(labels) / sizeof(labels[0]))));
    // Show the same scale on the left and right y-axes
    c->syncYAxis();
    // Set the left y-axis and right y-axis title using 10pt Arial Bold font
    c->yAxis()->setTitle("USD (millions)", "arialbd.ttf", 10);
    c->yAxis2()->setTitle("USD (millions)", "arialbd.ttf", 10);
    // Set y-axes to transparent
    c->yAxis()->setColors(Chart::Transparent);
    c->yAxis2()->setColors(Chart::Transparent);
    // Disable ticks on the x-axis by setting the tick color to transparent
    c->xAxis()->setTickColor(Chart::Transparent);
    // Set the label styles of all axes to 8pt Arial Bold font
    c->xAxis()->setLabelStyle("arialbd.ttf", 8);
    c->yAxis()->setLabelStyle("arialbd.ttf", 8);
    c->yAxis2()->setLabelStyle("arialbd.ttf", 8);
    // Output the chart
    c->makeChart("cylinderlightbar.jpg");
    //free up resources
    delete c;
    return 0;
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn