图表X轴编号 [英] chart x-axis numbering

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

问题描述

我正在使用WinForms图表来可视化一些数据。我想在我指定的点使用x轴网格线。请看下面的例子。

 公共局部类Form1:Form 
{
public Form1()
{
InitializeComponent();
AddPoints();
}

public void AddPoints()
{
for(int i = 0; i< = 100; i ++)
chart1.Series [ 0] .Points.AddXY(i,i);
}
}

在图表中,您可以看到X的网格线-轴出现在19、39、59、79和99。但是我想要0、15、45、65、90、100。您可以清楚地看到间隔是不一样的。因此,设置时间间隔是没有用的。



解决方案

使用 GridLines 不可能因为它们将始终以固定的 Interval 间距绘制。这是一个通过在 xxxPaint 事件中画线来解决的示例。



首先,我们声明一个列表我们想要的 GridLines 的止损值:

  List< double> Stops =新List< double>(); 

然后我们准备图表:

  AddPoints(); 

ChartArea ca = chart1.ChartAreas [0];
ca.AxisX.Minimum = 0; //可选
ca.AxisX.MajorGrid.Enabled = false;
ca.AxisX.MajorTickMark.Enabled = false;
ca.AxisX.LabelStyle.Enabled = false;

stop.AddRange(new [] {0,15,45,50.5,65,90,100});

请注意,我已经添加了一个额外的价值 50.5 ),以显示即使没有 DataPoints 的情况,我们也可以绘制 GridLines ! / p>

然后我们编写 PostPaint 事件的代码:

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

ChartArea ca = chart1.ChartAreas [0];

字体= ca.AxisX.LabelStyle.Font;
颜色col = ca.AxisX.MajorGrid.LineColor;
int padding = 10; //从轴

插入标签int y0 =(int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Minimum);
int y1 =(int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Maximum);

foreach(int sx in stop)
{
int x =(int)ca.AxisX.ValueToPixelPosition(sx);

使用(钢笔=​​新的Pen(col))
g.DrawLine(pen,x,y0,x,y1);

字符串s = sx +;
if(ca.AxisX.LabelStyle.Format!=)
s = string.Format(ca.AxisX.LabelStyle.Format,s);

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

结果如下:





请注意, PostPaint 事件只是准备;线条和标签的两个实际图形调用是普通的 GDI + 方法。



请注意,我已经添加了 DataPoint 标签到循环中的第10个点以显示我们的位置:

  chart1.Series [0] .Points.AddXY(i,i); 
if(i%10 == 0)chart1.Series [0] .Points [i] .Label = #VAL / #VALY;


I am using WinForms chart to visualize some data. I want x-axis gridlines at points specified by me. Please take a look at the following example.

public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();            
        AddPoints();
    }

    public void AddPoints()
    {
        for (int i = 0; i <= 100; i++)
            chart1.Series[0].Points.AddXY(i, i);
    }
}

In the chart you can see the grid lines for X-Axis appears at 19,39,59,79 and 99. But I want it in 0,15,45,65,90,100. You can clearly see the interval is not the same. So setting the interval is of no use. Is it possible to have grid lines in my own specified point?

解决方案

This is not possible with GridLines as they will always be drawn at the fixed Interval spacing. Here is an example of working around by drawing the lines in an xxxPaint event..

First we declare a list of stop values for the GridLines we want:

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

Then we prepare the chart:

AddPoints();

ChartArea ca = chart1.ChartAreas[0];
ca.AxisX.Minimum = 0;  // optional
ca.AxisX.MajorGrid.Enabled = false;
ca.AxisX.MajorTickMark.Enabled = false;
ca.AxisX.LabelStyle.Enabled = false;

stops.AddRange(new[] { 0, 15, 45, 50.5, 65, 90, 100 });

Note that I have added one extra value (50.5) to show how we can draw GridLines even where not DataPoints are!

Then we code the PostPaint event:

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

    ChartArea ca = chart1.ChartAreas[0];

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

    int y0 = (int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Minimum);
    int y1 = (int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Maximum);

    foreach (int sx  in stops)
    {
        int x = (int)ca.AxisX.ValueToPixelPosition(sx);

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

        string s =  sx + "";
        if (ca.AxisX.LabelStyle.Format != "") 
            s = string.Format(ca.AxisX.LabelStyle.Format, s);

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

This is the result:

Note that most of the code in the PostPaint event is just preparation; the two actual drawing calls for the lines and the labels are ordinary GDI+ methods..

Note that I have added DataPoint labels to every 10th point in the loop to show where we are:

chart1.Series[0].Points.AddXY(i, i);
if (i%10 == 0) chart1.Series[0].Points[i].Label = "#VAL / #VALY";

这篇关于图表X轴编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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