让柱形图在图表控件中重叠 [英] Let column charts overlap in Chart Control

查看:139
本文介绍了让柱形图在图表控件中重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题: 我需要在一个图表控件中显示多个列(一个图表区域中大约有七个系列).现在,当我有一个列"类型的图表时,所有七个列将并排显示.我想做的就是将它们重叠.这可能吗?

I have following issue: I need to show multiple columns in a chart control (about seven series in one chart area). Now when I have a chart of type "Column" all seven columns get shown side by side. What I want to do is to overlap them. Is this possible?

以下两种解决方案对我没有帮助:

The following two solutions didn't help me:

绘制重叠的柱状图或条形图

图表控制两个数据集条重叠

谢谢.

推荐答案

没有内置的方法.

  • 一种解决方法是打开3-d,但这将完全改变图表的外观.

  • One workaround is to turn on 3-d, but that will completely change the look of the chart..

另一种方法是所有者绘制图表.

由于列的大小没有公开,所以这对于列和条形类型来说并不完全容易.

This is not exactly easy for column and bar types, since the sizeof the columns is not exposed.

还要注意,重叠的列确实会变得更难阅读,尤其是.当你也有Labels.

Also note that overlapping columns do get somewhat harder to read, esp. when you also have Labels.

以下是所有者图纸柱形图的示例.它有几个简化:

Here is an example of a owner-drawing column chart. It has several simplifications:

所有Series具有相同数量的点并且对齐,所有y值均为正,并且没有其他修饰.它们可能都可以克服,但是可能需要付出额外的努力..

All Series have the same number of points and are aligned, all y-values are positive and there are no other adornments. They may all be overcome, but probably with some extra efforts..

private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
    if (!checkBox2.Checked) return;

    int sMax = chart1.Series.Count;
    ChartArea ca = chart1.ChartAreas[0];
    Axis ax = ca.AxisX;
    Axis ay = ca.AxisY;
    float py0 = (float)ay.ValueToPixelPosition(ay.Minimum);
    Rectangle ipr = Rectangle.Round(InnerPlotPositionClientRectangle(chart1, ca));
    int pMax = chart1.Series[0].Points.Count;
    float shift = (overlap * sMax) / 2f;
    float deltaX = 1f * ipr.Width / (pMax+1);
    float colWidth = 1f * deltaX / sMax;

    for (int j = 0; j < chart1.Series.Count; j++)
        for (int i = 0; i < chart1.Series[j].Points.Count; i++)
        {
            DataPoint dp = chart1.Series[j].Points[i];
            float px = (float)ax.ValueToPixelPosition(dp.XValue);
            float py = (float)ay.ValueToPixelPosition(dp.YValues[0]);
            using (SolidBrush brush = new SolidBrush(chart1.Series[j].Color))
                e.ChartGraphics.Graphics.FillRectangle(brush, 
                    px + j * colWidth - deltaX / 2 - overlap * j + shift,   py,
                    colWidth, py0 -  py );
        }
}

它使用了功能InnerPlotPositionClientRectangle,您可以在此处

It makes use of a function InnerPlotPositionClientRectangle which you can find here

这是结果:

请注意,要访问系列颜色,您需要

Note that to access the Series Colors you need to apply them to the Chart:

  chart1.ApplyPaletteColors();

列宽设置如下:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    for (int j = 0; j < chart1.Series.Count; j++)
        chart1.Series[j]["PointWidth"] = numericUpDown1.Value.ToString();
}

列为"0"时,列消失.

At "0" the columns disappear.

这篇关于让柱形图在图表控件中重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆