MS Chart X-Axis Scaling/Interval Issues
-
Tuesday, July 12, 2011 7:43 PM
Hey guys,
Am running Microsoft Visual Studios C# 2008 with the Chart Patch installed.
My program imports data from a CSV into a 2D array, which enters a DataSource with both columns (X and Y) taking on float values. When I go to display these value, my Y-axis is fine (starts at 0, goes up in even intervals), but my X axis seems scaled very poorly. My axis intervals were initally drawn in a random pattern (ie from left to right, 0, 50, 30, 100) which clearly makes no sense from a numerical basis. My solution to this was to use a quick sort on the array, by the X value, but this was not a great solution as the intervals are still off (ie, evenly spaced intervals from left to right might show 0, 2, 1300, 5000).
After conducting some research into it I'm pretty sure that the problem is that the Chart is recognizing my X-Axis as a Category type rather than a Numeric type, and so has no problem plotting each point (I'm running a point graph, scatter plot style) as its own categry bar. But I cannot for the life of me seem to find the proper command to force it to recognize my X-Axis, full of floats, as a numeric axis.
Here is some sample code: (leftCrossPlotGraph is the name of my chart, originalLeftCrossPlotData is my data source)
leftCrossPlotGraph.Series.Clear();
leftCrossPlotGraph.DataSource = originalLeftCrossPlotData;
//leftCrossPlotGraph.ChartAreas[0].AxisX.Minimum = 0;
//leftCrossPlotGraph.ChartAreas[0].AxisX.Interval = 20;
System.Windows.Forms.DataVisualization.Charting.
Series tempSeries = new System.Windows.Forms.DataVisualization.Charting.Series("tempSeries");
tempSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.
SeriesChartType.Point;
tempSeries.XValueMember = originalLeftCrossPlotData.Columns[0].ColumnName;
tempSeries.YValueMembers = originalLeftCrossPlotData.Columns[1].ColumnName;
//tempSeries.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Single;
leftCrossPlotGraph.Series.Add(tempSeries);
//leftCrossPlotGraph.ChartAreas[0].AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
leftCrossPlotGraph.ChartAreas[0].AxisX.Title = originalLeftCrossPlotData.Columns[0].ColumnName;
leftCrossPlotGraph.ChartAreas[0].AxisY.Title = originalLeftCrossPlotData.Columns[1].ColumnName;
As you can see in the code, I have tried several things to force it to recognize the X axis as numeric (and thus fix the mismatched scale) with no success... Any help would be greatly appreciated.
Thanks,
Brandon
All Replies
-
Tuesday, July 12, 2011 8:35 PM
bump. Twelve views and no one knows :S. It's got to be something as simple as a single line... I just can't seem to find the proper property that has to be changed.
-
Wednesday, July 13, 2011 12:09 PM
How about this:
Chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
horngsh -
Wednesday, July 13, 2011 3:23 PM
Nope, doesn't fix it. Thanks for trying though :S. I think that just fixes the number of interval bars. I'm not worried about how many interval bars I have. Again, the problem is that my x-axis is being treated as categorical rather than numerical. I need to figure out how to change that setting... any more suggestions?
-
Wednesday, July 13, 2011 4:01 PM
What is your data type of the column originalLeftCrossPlotData.Columns[0]?
That is, I mean the datat type of x-axis value's data source.
horngsh -
Wednesday, July 13, 2011 4:12 PM
It is of type float.
A snippet:
for
(int rowCounter = 0; rowCounter < crossPlots[indexA, indexB].numberOfPlotRows; rowCounter++)
{
DataRow aRow;aRow = originalLeftCrossPlotData.NewRow();
aRow[0] = crossPlots[indexA, indexB].plotData[rowCounter, 0];
aRow[1] = crossPlots[indexA, indexB].plotData[rowCounter, 1];
originalLeftCrossPlotData.Rows.Add(aRow);
}
plotData is a 2d array of floats.
-
Wednesday, July 13, 2011 6:42 PMGah. Have looked everywhere and can't find a solution, this is driving me crazy.
-
Thursday, July 14, 2011 9:31 AMCould you please detailly explain what you mean by AxisX being treated as Category and not as Numeric? I don't quite understand your question. IMHO, AxisX can be Single Value and can be set in the XValueType properties.
horngsh -
Thursday, July 14, 2011 3:57 PM
I took a quick screenshot.
http://i435.photobucket.com/albums/qq74/BakunaBakuna/Work%20stuff/screenshotOfSkewedGraphLines-1.jpg
On the left of the screenshot examine the graph. The equally spaced X-axis ticks go 0, 153.4, 165.4, 168.3, 170.4, 172.9. None of the intervals are equal, in particular, the first massive jump from 0 to 153.4.
What's more, the only reason that my points even seem to be ordered is that I cheated and used a quick sort on my original data, so that the points in the datasource would be listed in ascending x order. Without the quick sort, as I mentioned in my OP, the X-axis ticks jump randomly around as if each X-value is a Category.
For example, a Category set could be color {red, blue, green} versus a Y-value of say, picked as favorite color. The ordering of the colors on the X-axis doesn't really matter.
It is my believe that as of now, my code is interpreting the set of x values (ie {3,1,5,4,10}) as Category values, and so does not attempt to order them or properly scale the axis, as it would not make sense to do so.
What I want is for the x-values to be treated as singles (and yes, I tried setting type using
tempSeries.XValueType = System.Windows.Forms.DataVisualization.Charting.
ChartValueType.Single;)
All I want to do is display a normal graph... it should not be this difficult.
-
Thursday, July 14, 2011 4:16 PM
I think I managed to solve the problem. Somehow my datasource type was being force-classed to type string, rather than type single. Overriding it seems to plot the x vals on proper intervals, although now I have some scale manipulation to do.- Proposed As Answer by DGoodbody Monday, July 16, 2012 3:49 PM
- Marked As Answer by siplaModerator Monday, October 29, 2012 2:04 PM
-
Friday, July 15, 2011 2:40 AM
You can use these code to zoom in and out your x-axis scale:
this.Chart1.ChartAreas[0].AxisX.ScaleView.Zoom(StartXVal, EndXVal);
horngsh -
Wednesday, June 27, 2012 12:19 PMCan you elaborate on how you did this
-
Thursday, September 20, 2012 3:54 AMIMHO, it is better to have one post for one question. It is much clear that the center subject of the post would not be deviated if you do so.
My blog: http://soho-hsh.blogspot.com
-
Saturday, March 02, 2013 3:43 AM^This guy...This solved it for me. I created a MSDN account just to bump this. This was pissing me off for a long time. You sir, are my hero.

