图表列之间的网格 [英] Chart column grid between

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

问题描述

我使用System.Windows.Forms.DataVisualization.Charting。
现在我有一个这样的柱形图。





但我需要将网格放在列的旁边。



img src =https://i.stack.imgur.com/YX3cu.pngalt =Image>



我该如何做?






我的数据来自DataGridView,所以我的代码看起来像这样。

  var ser = chart1.Series.Add(ana); 
ser.IsValueShownAsLabel = true;
ser.IsVisibleInLegend = false;
ser.ChartType = SeriesChartType.Column;

// X值是字符串
ser.XValueMember = dataGridView1.Columns [0] .DataPropertyName;

// Y值是int
ser.YValueMembers = dataGridView1.Columns [1] .DataPropertyName;

chart1.DataSource = dataGridView1.DataSource;

DataGridView中的数据很简单。

 
-------------
| Month | Sales |
-------------
| Jan | 17 |
-------------
| Feb | 28 |
-------------
| Mar | 19 |
-------------


解决方案

code> GridLines 去与 DataPoints 标签去与 GridLines ,但是



可以设置 Interval code> 最小以调整它们:





但有一些额外的功能要注意..

  //获取引用
ChartArea chartArea1 = chart1.ChartAreas [0];

//不从0开始
chartArea1.AxisX.IsStartedFromZero = false;

//选择间隔
double interval = 1D;
chartArea1.AxisX.MajorTickMark.Interval = interval;

//在中间设置最小值
chartArea1.AxisX.Minimum = interval / 2d;

//选择列宽(可选)
chart1.Series [0] .SetCustomProperty(PixelPointWidth,30);

//我们希望标签与点坐标而不是网格线。
//因此我们为网格线之间的每个点添加自定义标签..
for(int i = 0; i {
DataPoint dp = chart1.Series [0] .Points [i]
chartArea1.AxisX.CustomLabels.Add((0.5d + i)* interval,
(1.5d + i)* interval,dp.XValue.ToString());
}

更新



如您更新的问题所示,您正在使用与字符串的数据绑定 X值。这是非常方便的,但违背了图表控件的核心性质。其所有的xalue,无论是 X-Value 或任何 Y-Values 内部存储为双精度。 p>

随机字符串不会转换为双精度,而且您可以方便地用字符串添加 DataPoints 因为 X-Values ,所有种类的丑陋问题都会出现。



先看看 X值自己:正如我所说的,他们是双;当你检查他们的值,你会看到他们都是0.字符串值在哪里?它们被放置在标签中。



一个问题经常发现,现在您无法使用


I'm using System.Windows.Forms.DataVisualization.Charting. Now I have a column chart like this.

But I need the grid to be aside to the column like this.

How can I do it?


My data are from DataGridView, so my code was look like this.

var ser = chart1.Series.Add("ana");
ser.IsValueShownAsLabel = true;
ser.IsVisibleInLegend = false;
ser.ChartType = SeriesChartType.Column;

//X value is string
ser.XValueMember = dataGridView1.Columns[0].DataPropertyName;

//Y value is int
ser.YValueMembers = dataGridView1.Columns[1].DataPropertyName;

chart1.DataSource = dataGridView1.DataSource;

And the data in DataGridView is simple.

-------------
|Month|Sales|
-------------
| Jan | 17  |
-------------
| Feb | 28  |
-------------
| Mar | 19  |
-------------

解决方案

GridLines go with DataPoints and Labels go with GridLines, but..

You can set the Interval and the Minimum for the X-Axis to tweak them apart :

but there are a few extras to watch out for..

// get a reference
ChartArea chartArea1 = chart1.ChartAreas[0];

// don't start at 0
chartArea1.AxisX.IsStartedFromZero = false;

// pick your interval
double interval = 1D;
chartArea1.AxisX.MajorTickMark.Interval = interval;

// set minimum at the middle
chartArea1.AxisX.Minimum = interval / 2d;

// pick a column width (optional)
chart1.Series[0].SetCustomProperty("PixelPointWidth", "30");

// we want the labels to sit with the points, not the grid lines..
// so we add custom labels for each point ranging between the grid lines..
for (int i = 0; i <  chart1.Series[0].Points.Count; i++)
{
    DataPoint dp = chart1.Series[0].Points[i];
    chartArea1.AxisX.CustomLabels.Add( (0.5d + i) * interval, 
                                       (1.5d + i) * interval, dp.XValue.ToString());
}

Update:

As your updated question shows, you are using data-binding with strings as X-Values. This is very convenient, but goes against the core nature of the Chart control. All its xalues, be it X-Value or any of the Y-Values are internally stored as doubles.

Random strings don't convert to double and while you can conveniently add DataPoints with strings as X-Values, all sorts of ugly issues come up as a consequence.

First have a look at the X-Values themselves: As I said they are double; when you check their values you will see they all are 0. And where are the string values? They are placed in the labels.

One problem often found it that you now can't access the X-Values with value expressions.

Another problem is that we now can't give a range of x-Values for custom labels.

Solution: If we need custom labels, we must change the data!

Data binding is fine, just make sure to add a numeric column to you datasource containing a month number and set it as the XValueMember of the series!

You can make that column it invisible in your DataGridView.

Finally you will want to create the custom labels, pretty much as before; just change their content to pull from the string column containing the month names.

Here is what it looks like here:

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

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