.Net图表-具有不同间隔的X轴 [英] .Net Charts - X Axis with Different Intervals

查看:105
本文介绍了.Net图表-具有不同间隔的X轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.Net图表.在这种情况下,我显示了一个间隔为28天的折线图.

这是我的代码:

 Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;
 Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
 Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
 Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 28;

但是,我的情况之一是

28天间隔,35天间隔,28天间隔等.是否可能具有不同的间隔?

解决方案

IntervalAxis属性,并且只能一个. /p>

您可以通过自己绘制网格线和标签来解决此限制.


让我们假设您有一个停止点列表,即要在其中显示GridLineDataPoints的索引:

List<int> stops = new List<int>();

添加一些测试编号stops.AddRange(new[] { 12, 23, 42, 52, 82 });之后,我们可以编写ChartPostPaint事件以绘制线条:

private void chart_PostPaint(object sender, ChartPaintEventArgs e)
{
    Graphics g = e.ChartGraphics.Graphics;
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

    ChartArea ca = chart.ChartAreas[0];

    Font font = ca.AxisX.LabelStyle.Font;
    Color col = ca.AxisX.MajorGrid.LineColor;
    int padding = 10; // pad the labels from the axis

    double aymin = ca.AxisY.Minimum;
    double aymax = ca.AxisY.Maximum;

    int y0 = (int)ca.AxisY.ValueToPixelPosition(aymin);
    int y1 = (int)ca.AxisY.ValueToPixelPosition(aymax);

    foreach (int sx  in stops)
    {
        int x = (int)ca.AxisX.ValueToPixelPosition(chart.Series[0].Points[sx].XValue);

        using (Pen pen = new Pen(col))
            g.DrawLine(pen, x, y0, x, y1);

        string s =  chart.Series[0].Points[sx].XValue + "";
        if (ca.AxisX.LabelStyle.Format != "") s = string.Format(ax.LabelStyle.Format, s);

        SizeF sz = g.MeasureString(s, font, 999);
        g.DrawString(s, font, Brushes.Black, (int)(x - sz.Width / 2) , y0 + padding);
    }
}

关闭原始的MajorGrid等后.

ChartArea ca = chart.ChartAreas[0];

ca.AxisX.MajorGrid.Enabled = false;
ca.AxisX.MajorTickMark.Enabled = false;
ca.AxisX.LabelStyle.Enabled = false;

..这是结果:

注意:

  • 大多数代码只是简单的准备和参考.实际的绘图是2种方法以及另外3或4条线来获取坐标..

  • 我已将DataPoint 索引存储在我的列表中.如果希望自定义GridLines独立于DataPoints,则可以存储Values并将列表更改为List<double>,并将两个引用从chart.Series[0].Points[sx].XValue更改 直接访问停止值sx.

  • 更改填充值以适合您..

  • 即使实际上将它们设置为Auto,我们也可以自由访问轴的最小值和最大值.这是因为我们处于Paint事件中.否则,我们将不得不在ChartArea上调用RecalculateAxesScale()..

  • 还可以随意使Black标签刷动态化..

I am using .Net Charts. In that, I have displayed a line chart with an Interval of 28 Days.

Here is my code:

 Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;
 Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
 Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
 Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 28;

But, one of my situation comes like,

28 Days Interval, 35 Days Interval, 28 Days Interval etc. Is that possible to have different Intervals.

解决方案

No, Interval is an Axis property and there can only be one.

You can work around this restriction by drawing gridlines and labels on your own.


Let's assume you have a list of stop points, i.e. the indices of the DataPoints where you want a GridLine to appear:

List<int> stops = new List<int>();

After adding a few test numbers stops.AddRange(new[] { 12, 23, 42, 52, 82 }); we can code the PostPaint event of the Chart to draw lines:

private void chart_PostPaint(object sender, ChartPaintEventArgs e)
{
    Graphics g = e.ChartGraphics.Graphics;
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

    ChartArea ca = chart.ChartAreas[0];

    Font font = ca.AxisX.LabelStyle.Font;
    Color col = ca.AxisX.MajorGrid.LineColor;
    int padding = 10; // pad the labels from the axis

    double aymin = ca.AxisY.Minimum;
    double aymax = ca.AxisY.Maximum;

    int y0 = (int)ca.AxisY.ValueToPixelPosition(aymin);
    int y1 = (int)ca.AxisY.ValueToPixelPosition(aymax);

    foreach (int sx  in stops)
    {
        int x = (int)ca.AxisX.ValueToPixelPosition(chart.Series[0].Points[sx].XValue);

        using (Pen pen = new Pen(col))
            g.DrawLine(pen, x, y0, x, y1);

        string s =  chart.Series[0].Points[sx].XValue + "";
        if (ca.AxisX.LabelStyle.Format != "") s = string.Format(ax.LabelStyle.Format, s);

        SizeF sz = g.MeasureString(s, font, 999);
        g.DrawString(s, font, Brushes.Black, (int)(x - sz.Width / 2) , y0 + padding);
    }
}

After turning off the original MajorGrid etc..

ChartArea ca = chart.ChartAreas[0];

ca.AxisX.MajorGrid.Enabled = false;
ca.AxisX.MajorTickMark.Enabled = false;
ca.AxisX.LabelStyle.Enabled = false;

..this is the result:

Notes:

  • Most of the code are just simple preparations and references. The actual drawing are 2 methods and three or four more lines to get the coordinates..

  • I have stored the DataPoint indices in my List. If you want the custom GridLines to be independent of DataPoints you can instead store the Values and change the List to a List<double> andthe two references from chart.Series[0].Points[sx].XValue to accessing the stop values sx directly.

  • Change the padding value to suit you..

  • We can access the axes' minima and maxima values freely, even if they are actually set to Auto. This is because we are in a Paintevent. Otherwise we would have to call RecalculateAxesScale() on the ChartArea..

  • Feel free to make the Black label brush dynamic as well..

这篇关于.Net图表-具有不同间隔的X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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