Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

c# - Chart column grid between

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

image

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

image

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  |
-------------
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :

enter image description here

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...