I've made a dynamic 3d piechart using devexpress. I'm really impressed with how good the control feature is. I've hit a little bit of a downer though.
I would like my pie chart points to have different colors that I set in code (this will later be changed by the user using some form of pallet or combo box, not sure yet). Unfortunatly I can't seem to get a color method for the points of my data series.
Here's the code excluding the mass of commented out attempts:
Series series1 = new Series("Series1", ViewType.Pie3D);
chartControl2.Series.Add(series1);
series1.DataSource = chartTable;
series1.ArgumentScaleType = ScaleType.Qualitative;
series1.ArgumentDataMember = "names";
series1.ValueScaleType = ScaleType.Numerical;
series1.ValueDataMembers.AddRange(new string[] { "Value" });
//series1.Label.PointOptions.PointView = PointView.ArgumentAndValues;
series1.LegendPointOptions.PointView = PointView.ArgumentAndValues;
series1.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
series1.LegendPointOptions.ValueNumericOptions.Precision = 0;
// Adjust the value numeric options of the series.
series1.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
series1.Label.PointOptions.ValueNumericOptions.Precision = 0;
// Adjust the view-type-specific options of the series.
((Pie3DSeriesView)series1.View).Depth = 20;
((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[0]);
((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[1]);
((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[2]);
((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[3]);
((Pie3DSeriesView)series1.View).ExplodedDistancePercentage = 20;
chartControl2.Legend.Visible = true;
So I need something like chartcontrol2.series1.point[0].color = color.blue; something like this.
Drawing in charts when drawing the series points of charts. To do this you should handle the ChartControl.CustomDrawSeriesPoint event, and then you can change some of the drawing parameters using its event args.
check these events to do your functinality..
How to: Custom Paint Series Points
ChartControl.CustomDrawSeries Event
You need to define a chart Palette or use an existing one defined by DevExpress. See this
http://documentation.devexpress.com/#XtraCharts/CustomDocument7434
Related
I'm trying to add a Chart control dynamically to a Form (using C#, this should all be .NET 4.0), but it's always blank (only the background color shows). I tried the same code in a Form that already has a control and it works, so I imagine it's some initialization function I should call (I tried Invalidate() on the control and Refresh() on both control and the panel it's being placed in, made no difference). I went through the few similar posts I found, tried throwing in any other commands they used (BeginInit() is from one such post) but no luck so far. Any ideas?
BTW I want to display 6-9 charts (position, speed and acceleration in 3D space) so I'd rather add them dynamically than have 9 sets of assignments. Here's the code that adds the charts to the panel:
foreach (KeyValuePair<string, List<double>> p in b.storedValues)
{
Control c = getChartForData(p);
panel1.Controls.Add(c);
c.Invalidate();
c.Refresh();
break;
}
And the function that creates each chart:
private Chart getChartForData(KeyValuePair<string, List<double>> data)
{
Chart c = new Chart();
((System.ComponentModel.ISupportInitialize)c).BeginInit();
c.Series.Clear();
c.BackColor = Color.White;
c.Height = 300;
c.Width = 500;
c.Palette = ChartColorPalette.Bright;
Series s = new Series(data.Key);
s.ChartType = SeriesChartType.Spline;
double maxValue = 0;
//NOTE: Going logarithmic on this, too big numbers
for (int i = 0; i < data.Value.Count; i++)
{
maxValue = Math.Max(Math.Log10(data.Value[i]), maxValue);
}
for (int i = 0; i < data.Value.Count; i++)
{
s.Points.AddXY(i,Math.Log10(data.Value[i]) * c.Height / maxValue);
}
c.Series.Add(s);
return c;
}
Many thanks in advance.
When you create a Chart yourself, in code, it does not contain any ChartArea.
Therefore, nothing is displayed.
I'm guessing that the designer generates some code to initialize a default chart area when you drag and drop a chart control onto the form.
Also, you should really let the chart control handle the layout, instead of calculating the desired position of each point based on the height of the chart control.
I would go as simple as possible to get something that's working, and then you can tweak the range of the axis afterwards. You can also set an axis to be logarithmic.
Start with trying out this minimal version, and make sure that displays something, before you complicate things. This works for me.
private Chart getChartForData(string key, List<double> data)
{
Chart c = new Chart();
Series s = new Series(key);
s.ChartType = SeriesChartType.Spline;
for (int i = 0; i < data.Count; i++)
{
s.Points.AddXY(i, data[i]);
}
c.Series.Add(s);
var area = c.ChartAreas.Add(c.ChartAreas.NextUniqueName());
s.ChartArea = area.Name;
// Here you can tweak the axis of the chart area - min and max value,
// where they display "ticks", and so on.
return c;
}
I'm familiar with C# and Winform, but new to Chart control. I'm using VS2013 built in System.Windows.Forms.DataVisualization.Charting.Chart to display bar for two points. Here's simply the code:
private void Form1_Load(object sender, EventArgs e)
{
Chart c = new Chart();
c.Dock = DockStyle.Fill;
ChartArea a = new ChartArea();
a.AxisX.Minimum = 0;
a.AxisX.Maximum = 4;
a.AxisY.Minimum = 0;
a.AxisY.Maximum = 2;
c.ChartAreas.Add(a);
Series s = new Series();
//*******************
s.Points.AddXY(1, 1);
//s.Points.AddXY(2, 2);
s.Points.AddXY(3, 2);
//*******************
c.Series.Add(s);
this.Controls.Add(c);
}
Please note the commented part, the points (2,2) and (3,2) are only different data, and have nothing to do with display style(I guess?). So this behavior seems very strange and so far I haven't found any solution to keep displaying (3,2) like (2,2).
You need to add the below codes in order to achieve the desired output:
1] Fix the interval for the Axis so the axis label and Grid lines do not change.
a.AxisX.Interval = 1;
2] Fix the bar width for the series. You can use PixelPointWidth to specify the width in Pixels or PointWidth in Points.
Example using PixelPointWidth:
s["PixelPointWidth"] = "20";
Also, since you are using c.Dock = DockStyle.Fill; to fill the chart into whole form, fixed width will not be good if you scale the form.
You can use MinPixelPointWidth and MaxPixelPointWidth to give the range to the width.
s["MinPixelPointWidth"] = "20";
s["MaxPixelPointWidth"] = "80";
Check this link for details on different chart elements.
Technical Reference for detailed documentation on Chart controls. This may be long but important to understand the details.
On my Chart, I want to put an obvious red vertical bar which goes from a specific point on the plot down to the x-axis. Is there a way to do this? Based on the documentation, it doesn't seem like this option is available or perhaps I'm looking in the wrong area.
The most obvious way is to add a VerticalLineAnnotation.
Here is an example:
First I set up a few things:
int yourPointIndex = 635;
Series S1 = chart1.Series[0];
ChartArea CA1 = chart1.ChartAreas[0];
Now I create the Annotation and style it a little:
VerticalLineAnnotation LA = new VerticalLineAnnotation();
LA.LineColor = Color.Red;
LA.LineWidth = 9;
LA.IsInfinitive = false;
LA.AnchorDataPoint = S1.Points[yourPointIndex]; ;
Now I position it with the Point in question:
LA.X = S1.Points[yourPointIndex].XValue;
LA.Y = S1.Points[yourPointIndex].YValues[0];
// this makes the bar go down to the zero axis
LA.Height = LA.Y;
// this makes it go down all the way to the x-axis:
LA.Height = LA.Y - CA1.AxisY.Minimum;
// we should clip it to our chartarea:
LA.ClipToChartArea = CA1.Name;
Finally it is added to the Annotations collection of the Chart.
chart1.Annotations.Add(LA);
Note that Annotations can be adorned and made to be moveable..
Note: The code above was written for and tested with Winforms but the MS Chart control is rather similar in all its versions..
I am using the chart component of .NET Framework 4.5 in C#.
When I fill that component with values that do not have the same gap to each other and set the ChartArea.Area3dStyle.Enable3d property to true, there is almost no impact on the chart until I equalize the gaps between the values in the series.
DataPoint dataPoint1 = new DataPoint(0D, 20D);
DataPoint dataPoint2 = new DataPoint(1D, 30D);
DataPoint dataPoint3 = new DataPoint(200D, 50D);
DataPoint dataPoint4 = new DataPoint(300D, 20D);
DataPoint dataPoint5 = new DataPoint(400D, 0D);
DataPoint dataPoint6 = new DataPoint(500D, 30D);
DataPoint dataPoint7 = new DataPoint(600D, 10D);
DataPoint dataPoint8 = new DataPoint(700D, 10D);
series1.Points.Add(dataPoint1);
series1.Points.Add(dataPoint2);
series1.Points.Add(dataPoint3);
series1.Points.Add(dataPoint4);
series1.Points.Add(dataPoint5);
series1.Points.Add(dataPoint6);
series1.Points.Add(dataPoint7);
series1.Points.Add(dataPoint8);
chartArea1.Area3DStyle.Enable3D = true;
You can set various parameters here. The one that directly makes up for the automtic downscaling with your large x-values is PointDepth
chartArea1.Area3DStyle.Enable3D = true;
chartArea1.Area3DStyle.PointDepth = 999; // pick a value you like
The best way to explore the many wonders of the Chart control is to play with it in the designer. When you have found a good setting you can go to the yourForm.Designer.cs file and look at the generated code; while you are not advised to fool around with it is a valuable resource for many obscure settings..
Unfortunately the maximum PointDepth is 1000 and with x-values larger than 300-500 the 3D-effect is still rather weak. Maybe you can downscale your values?
So I'm using Windows Forms Chart to generate graphs containing several lines that can create some clutter on the graph and need something to differentiate them other than color. There are too many points to using dotted or dashed lines as there is no observable difference between that and a continuous line. So what I'm hoping to do is to get markers with various shapes to show up on the lines like in Excel, for instance. Right now I'm have it coded like this
myChart.Series["MySeries"].ChartType = SeriesChartType.FastLine;
myChart.Series["MySeries"].MarkerStyle = MarkerStyle.Diamond;
What this does is put a diamond in the legend over that line, but it doesn't put diamonds on the actual line that is in the chart itself. Changing the marker size doesn't make a difference, unfortunately, and neither does changing the marker color. Is there a way to get that to happen. Thanks for reading, and any help you have.
EDIT:
Heres the relevant code.
Its data is held in a class that is the value-peice of a dictionary.
The class contains a list of doubles.
public void Charter(Color colorOfLine)
{
double xValue;
double yValue;
myChart.Series.Add("MySeries");
myChart.Series["MySeries"].ChartType.FastLine;
myChart.Series["MySeries"].ChartArea = "ChartArea1";
myChart.Series["MySeries"].Color = colorOfLine;
myChart.Series["MySeries"].MarkerStyle = MarkerStyle.Diamond;
myChart.Series["MySeries"].MarkerColor = Color.Black;
myChart.Series["MySeries"].MarkerSize = 5;
myChart.Series["MySeries"].MarkerBoarderColor = Color.DeepPink;
foreach (KeyValuePair<int, MyClass> Pair in MyDictionary)
{
xValue = Pair.Value.MyClassList[0];
yValue = Pair.Value.MyClassList[1];
myChart.Series["MySeries"].Points.AddXY(xValue, yValue);
}
}
I should add that I've played around with the MarkerStep, and MarkerBoarderWidth as well, all to no benefit. The issue seems to be that the marker simply isn't appearing on the actual lines in the chart itself. Also I'm using Visual Studio 2010 Express for what its worth. Thanks again for reading.
Use Line. Don't use FastLine. FastLine won't generate markers for you.
myChart.Series["MySeries"].ChartType = SeriesChartType.Line
Set the MarkerSize to something bigger:
myChart.Series["MySeries"].MarkerSize = 4;
ETA:
You may also need to set the color of the marker:
myChart.Series["MySeries"].MarkerColor = Color.Blue;
myChart.Series["MySeries"].Color = Color.Blue;