【问题标题】:C# winform advance chartC# winform 进阶图
【发布时间】:2017-01-15 06:55:02
【问题描述】:

我想在 C# winform 中制作这样的图表控件

但我找不到任何可以像这样添加多个 XY 值的组件。 我怎样才能做这些东西? 我试过使用 Dev XtraChart。 但不能添加多个 xy 值。

【问题讨论】:

  • 说明:多个 XY 值
  • 多个 X 和 Y 值。通常我们只为此使用一个值(例如,X 的日期时间和 Y 的整数值)。但是我的一个项目需要为此分配多个,如您在图片中看到的(时间、日历月、I 级、II 级)X 轴和 Y 轴(50、100、150)。
  • 啊,我明白你的意思了。实际上我会称这些不是“价值”而是“标签”。 MsCharts 不太支持向图表添加多行漂亮的标签。这对两个轴都是正确的。我看到你也有一个复杂的 Y 轴标签布局.. - 我想我要么使用 GDI+ 方法将它们添加到 PostPaint 事件中,要么使用 DataGridView 并以某种方式将图表或其图形插入其中..
  • 当然你可以使用 DevExpress 图表来做到这一点。您需要添加多个系列,每个系列都有自己的视图(如线、点或条等)。见documentation.devexpress.com/#WindowsForms/CustomDocument7374
  • @Peter:如果仅此而已,那么普通的 MSChart 也可以。但是该示例在底部显示了完全不同的内容(四行标签),您提到的任何 ChartType 都不会创建这些内容!不过,我不知道 DevExpress Charts。 - 嗯,再想一想,可能会尝试几个系列类型的 AreaChart,但 imo 这将是一个令人毛骨悚然的解决方法。

标签: c# winforms charts


【解决方案1】:

如果您可以使用第三方图表库,您可以考虑ChartDirector。它允许将轴标签视为表格,并且您可以在表格中插入额外的行和列。

上面的图表是根据 ChartDirector 发行版中包含的示例代码之一进行修改的。原始示例代码位于http://www.advsofteng.com/doc/cdnet.htm#datatable.htm。修改后的代码如下。

// The data for the line chart
double[] data0 = { 42, 49, 33, 38, 64, 56, 29, 41, 44, 57, 59, 42 };
double[] data1 = { 65, 75, 47, 34, 42, 49, 73, 62, 90, 69, 66, 78 };
double[] data2 = { 36, 28, 25, 28, 38, 20, 22, 30, 25, 33, 30, 24 };

// Create a XYChart object of size 600 x 300 pixels
XYChart c = new XYChart(600, 300);

// Tentatively set the plotarea at (50, 10) and of (chart_width - 100) x (chart_height - 80)
// pixels in size. Set both horizontal and vertical grid lines to dotted semi-transprent black
// (aa000000).
PlotArea plotArea = c.setPlotArea(50, 10, c.getWidth() - 100, c.getHeight() - 80,
    -1, -1, -1, c.dashLineColor(unchecked((int)0xaa000000), Chart.DotLine), -1);

// y-axis labels
string[] yLabels = { "50", "100", "150", "50", "100", "150", "50", "100", "150", "50", "100", "150" };
c.yAxis().setLabels(yLabels);

// Convert the y-axis labels to a table
CDMLTable yTable = c.yAxis().makeLabelTable();
yTable.getStyle().setMargin(5, 5, 0, 0);

// Right alignment for the first and only column in the table
yTable.getColStyle(0).setAlignment(Chart.Right);

// Insert one more column
yTable.insertCol(0);
for (int i = 0; i < 4; ++i)
    yTable.setCell(0, i * 3, 1, 3, "" + (i + 2) + "(A)");

// x-axis labels
string[] xLabels = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
c.xAxis().setLabels(xLabels);

// Convert the x-axis labels to a table
CDMLTable xTable = c.xAxis().makeLabelTable();
xTable.getStyle().setMargin2(0, 0, 3, 3);

// Set the colors of the first and only row, and also that of the plot area background
int[] colors = { 0xffdddd, 0xddffdd, 0xddddff };
for (int i = 0; i < 3; ++i)
{
    for (int j = i * 4; j < (i + 1) * 4; ++j)
        xTable.getCell(j, 0).setBackground(colors[i], 0x000000);
    c.xAxis().addZone(i * 4 - 0.5, (i + 1) * 4 - 0.5, colors[i]);
}

// Add one more row
xTable.appendRow();
xTable.setCell(0, 1, 3, 1, "Q1");
xTable.setCell(3, 1, 3, 1, "Q2").setBackground(0xff99ff, 0x000000);
xTable.setCell(6, 1, 3, 1, "Q3");
xTable.setCell(9, 1, 3, 1, "Q4").setBackground(0xff9900, 0x000000);

// Add another row
xTable.appendRow();
xTable.setCell(0, 2, 4, 1, "Alpha").setBackground(0xffff00, 0x000000);
xTable.setCell(4, 2, 6, 1, "Beta").setBackground(0x99cc99, 0x000000);
xTable.setCell(10, 2, 2, 1, "Gamma").setBackground(0x99ccff, 0x000000);

// Insert a column on the left for the row names
xTable.insertCol(0).setMargin2(5, 5, 0, 0);
xTable.setText(0, 0, "Name 1");
xTable.setText(0, 1, "Name 2");
xTable.setText(0, 2, "Name 3");

// Set the width of the row names column to the same width as the y-axis table
xTable.getColStyle(0).setWidth(yTable.getWidth());

// Add a line layer to the chart
LineLayer layer = c.addLineLayer2();
layer.setUseYAxis2();
c.yAxis2().setLinearScale(0, 100, 10);
c.yAxis2().setColors(Chart.Transparent, Chart.Transparent);

// Set the line width to 3 pixels
layer.setLineWidth(3);

// Add the three data sets to the line layer, using circles, diamands and X shapes as symbols
layer.addDataSet(data0, 0xff0000, "Quantum Computer").setDataSymbol(Chart.CircleSymbol, 9);
layer.addDataSet(data1, 0x00ff00, "Atom Synthesizer").setDataSymbol(Chart.DiamondSymbol, 11);
layer.addDataSet(data2, 0xff6600, "Proton Cannon").setDataSymbol(Chart.Cross2Shape(), 11);

// Adjust the plot area size, such that the bounding box (inclusive of axes) is 2 pixels from
// the left, right and bottom edge, and is just under the legend box.
c.packPlotArea(2, 10, c.getWidth() - 3, c.getHeight() - 3);

// Output the chart
viewer.Chart = c;

//include tool tip for the chart
viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='[{dataSetName}] {xLabel}: {value}'");

【讨论】:

  • 正是我需要的,谢谢哥们!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
  • 2020-05-28
相关资源
最近更新 更多