I am attempting to have a Silverlight chart (windows phone 7) that a user can modify by changing some settings.
Upon clearing the charts axes and readding new axes, i end up with double axes being being reported by chart.ActualAxes.Count
does anyone know how to fully clear and delete all the axes on a silverlight chart and add new ones? Am i meant to call something to update the ActualAxes list after adding?
thanks in advance
code sample (call this twice and your chart will end up with 4 axes instead of two):
chart.Axes.Clear();
chart.Axes.Add(new LinearAxis()
{
Orientation = AxisOrientation.Y,
Location = AxisLocation.Left,
Minimum = 0
});
chart.Axes.Add(new DateTimeAxis()
{
Orientation = AxisOrientation.X,
Location = AxisLocation.Bottom,
IntervalType = DateTimeIntervalType.Days
});
The Chart Axes collection represents the persistentAxes in the chart that will be rendered even if there are no series in the chart. The ActualAxes represents a combination of both the persistent Axes and those in use by the series in the chart.
When you Clear the collection and test ActualAxes.Count you will find it still says 2 even though Axes is now 0. The ActualAxes (an instance of SeriesHostAxesCollection) will not allow the removal of an axis which is in use be an existing Series. Hence the ActualAxes collection holds on to the originals. You then add 2 others to the persistent Axes collection so those 2 new ones are also added to the ActualAxes, you end up with 4.
Run your code yet again (a third time) and you should see the ActualAxes count remains 4. Thats because the 2 Axis you added in the second call are not being used by any series so they can be removed from the ActualAxes collection.
Following code should work
var xAxis = chart_name.ActualAxes.OfType<LinearAxis>().FirstOrDefault(ax => ax.Orientation == AxisOrientation.X);
if (xAxis != null)
{
xAxis.Title = "The Title";
}
Related
Apologies for not able to find a more suitable title for my issue here. I am using MS Chart control in my project to plot real-time values from a source that generates 4 values per second. X axis is time at which the value is generated and the Y axis the value itself. I am dynamically adding a new series for each variable. All the variables are manually added by the user one-by-one and then a series will be created for it.
Basic part seems to be working except the chart is plotting values from different series at different time even though the values are generated at same time. Following image shows the exact issue:
As you can see, the list in bottom is a list of values I am plotting. It shows values and the time they are generated at (hh:mm:ss.ms)
When I plot these values, which is shown in the chart area, this is what I get. See the location of the latest values (marked by red arrows) for all the series. The latest value is at different time even though they all are generated at the same time. Every series seem to have a separate X axis. I expected the latest Y values should be plotted at the same X value and that should be shown at the extreme right of the chart. More like the bottom most series is being shown. That is the very first series created. Every time only the series that is created first works fine and the following series lags for some reason.
Chart, share X axis between different series looks like similar issue to mine but is not answered.
Chart Multiple series different length generate duplicate x axis seems to be the exact issue I am having but the difference is - I am plotting a real-time graph. I can't loop through every series after adding a new data point to mark whether a point is empty or not. That will be a lot of overhead. Some Googling suggested use of InsertEmptyPoints() to every series to match all the X axis. MS Chart sample code also explains using this method but I am not sure where/when to use this method. If I use it after creating a series but before adding a data point - the code crashes. I have tried something like this:
_chart.Series.Add(series);
_chart.DataManipulator.InsertEmptyPoints(250, IntervalType.Milliseconds, name);
Am I doing something wrong here? Thanks for any help offered.
I got this working by adding empty points. I filled every series with max possible data points to begin with. Say I want to show 100 data points in every series before I start deleting the oldest (Points[0]) data point so that my chart will look like it's moving from right to left. In that case I will add 100 empty points while creating every series using:
for (int i = 0; i < 100; i++)
{
series.Points.AddXY(i.ToString(), double.NaN);
series.Points[i].AxisLabel = " ";
}
Then, while adding real data just do it normal way:
_chart.Series[index].Points.AddXY(time, YValue);
// delete more than max. points.
if (_chart.Series[index].Points.Count > MaxTrendPoints - 10) // -10 is adjustment. Got it by trial and error.
_chart.Series[index].Points.RemoveAt(0);
Basically my C# chart control has a datetime type x axis.
I am plotting 2 different time period, but do not want the empty points to even show up in the x axis.
Is it possible to change a certain property of the chart control to hide the period of time which does not have point.
I implemented something similar to your problem, here is what I used (C#), it may help:
Chart4.DataManipulator.InsertEmptyPoints(1, System.Web.UI.DataVisualization.Charting.IntervalType.Number, Chart4.Series["s3"]);
Chart4.DataManipulator.InsertEmptyPoints(1, System.Web.UI.DataVisualization.Charting.IntervalType.Number, Chart4.Series["s2"]);
Chart4.Series["s3"].EmptyPointStyle.Color = System.Drawing.Color.Transparent;
Chart4.Series["s2"].EmptyPointStyle.Color = System.Drawing.Color.DarkGreen;
You can specify the intervalType.Days in place of number..
datamanipulator.ignoreemptypoints = true;
from this article on grouping:
http://msdn.microsoft.com/en-us/library/dd489213.aspx
If you don't want the x-axis to be scaled to the x-values simply set the/all series to be indexed:
foreach (Series s in chart1.Series) s.IsXValueIndexed = true;
Now each DataPoint will sit at its index position instead of its x-value position. Note that this also mean that you need to make sure all Series aligned, read all have a DataPoint at the same indices, be it a real one or an Empty one or else the will shift..!
I have a chart with 4 series. Each series is added at different times, depending on what is switched on/off of the app features. All series have on the x axis:
DateTime.Now.ToString("mm:ss")
so I thought that at any time the series data are available, they will be added to the chart at that time that happens on x axis. Looks like is not like that. This chart shows a blue line and a red line. The blue line started first then after few seconds I checked checkBox2 which activate the red line, that happened exactly at 27:38 (where you can see a small drop on the blue line). I do not understand why the red line starts at the far left of the chart and not at the time that was triggered (27:38).
This is my code:
string reqTemp = textBox9.Text;
textBox2.Text = avTemp.ToString("F");
this.chart1.Series["Actual Temperature"].Points.AddXY(DateTime.Now.ToString("mm:ss"), avTemp);
if (checkBox2.Checked == true)
{
this.chart1.Series["Requested Temperature"].Points.AddXY(DateTime.Now.ToString("mm:ss"), reqTemp);
}
How can I have the series added after the first one was already running starting at the time they are switched on? Basically all series sharing the same x axis.
All series have on the x axis:
DateTime.Now.ToString("mm:ss")
I read this as: All your X-Values are added as formatted strings; this is usually a bad thing, as by doing so the X-Values have become all 0s, read: meaningless.
If you want to preserve the DateTime values you need to add the DataPoints with valid X-Values!
So you should add them as AddXY(yourDateTimeXValue, yourYValue); and set the Format as
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";
To explain my question I drew an image, here it is:
So I have a Chart that has a bar graph and a line chart, 2 in 1. I am trying to displaly it in asp.net just the way i drew it on here. This means the A box on the image has a set of values which reflect the line graph, and C box on the image has a set of values that define the bar graph values.
What I want to ask is how do I set two different sets of values on a single chart. I know I can use series to draw multiple graphs and by having series defined as different types I can change if its a bar graph or a ilne chart, however I don't know how to add 2 range of values on the side.
As for the the box B, what I wish to ask is how do I set these values so that they are showing different dates, I was able to do it using a line of code shown bellow, however that line of code sets every value to same, and I need them to reflect different values such as range of dates in this example on the image.
Code that I tried is: Chart1.Series["Series1"].AxisLabel = "Test";
Help is most appreciated!
Cheers!
Ok so i'm going to leave this question up for those that come into similar problem.
This is how you set column A and column c
Chart1.Series[0].YAxisType = AxisType.Primary;
Chart1.Series[1].YAxisType = AxisType.Secondary;
Chart1.ChartAreas[0].AxisY.Maximum = 500;
Chart1.ChartAreas[0].AxisY2.Maximum = 7000;
Your grid lines might get messed up so if you want to disable them use
Chart1.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
And just as a bonus if you want to set intervals on each Y axis you can do that with
Chart1.ChartAreas[0].AxisY.Interval = 50;
I still haven't figured out how to do part B that I mentioned, so if you know the answer then please share.
Cheers!
I'm using the Visiblox WPF API and am having trouble getting the chart points in my line chart to scroll horizontally. Instead of scrolling, the points are get squashed together, in which, this isn't particularly a problem, except that I expect to have 100s of data points on the chart. I have looked throughout the examples available on the Visiblox website, but couldn't find what I was looking for. Ive attached an example screenshot.
Any ideas?
Thanks for your help,
Sparky
By default Visiblox Charts will re-calculate the range to include all the data in the series, so there are two possible approaches: 1) when you add the last point, remove the first one which will effectively move the visible window one point over or 2) set an explicit axis range and update that when you want to move the visible window.
Check out the Visiblox blog for more details on how ranges work at: http://www.visiblox.com/blog/2011/03/visiblox-charts-ranges-demystified
I just had something like this recently. Everytime I'd add a point to the cart I'd run a little section of code that would check the amount of time (my x-axis dimension) that had passed since 0. I also set up a range of data I always wanted to see. I always wanted to show 120 seconds of data on the graph. So I had something like this:
private void adjustXasis(int timeCount)
{
if(timeCount>desiredRange)
{
chart.axis.Xaxis.minimum=timeCount-desiredRange;
chart.axis.Xaxis.maximum=timeCount;
}
else //two minutes not reached yet
{
chart.axis.Xaxis.minimum=0;
chart.axis.Xaxis.maximum=desiredRange;
}
}
I don't have VS in front of me and I know that syntax for the axis min/max is wrong but you get the idea.
By default Visiblox Charts will re-calculate the range to include all the data in the series, so there are two possible approaches:
1) when you add the last point, remove the first one which will effectively move the visible window one point over or
2) set an explicit axis range and update that when you want to move the visible window.
Check out the Visiblox blog for more details on how ranges work at: http://www.visiblox.com/blog/2011/03/visiblox-charts-ranges-demystified