Separate Legends for Multiple ChartAreas in Single Chart - c#

I cannot seem to get my individual subcharts (contained within my single chart object) to host individual legends for their respective data series. I would like to know if this is possible and, if so, what I can adjust in my code to achieve this effect.
Thanks for your help
Code is as follows:
chart_MyChart.Legends.Clear();
ChartArea chartArea_MyData = new ChartArea("My Data");
ChartArea chartArea_YourData = new ChartArea("Your Data");
ChartArea chartArea_OtherData = new ChartArea("Other Data");
chart_MyChart.ChartAreas.Clear();
chart_MyChart.ChartAreas.Add(chartArea_MyData);
chart_MyChart.ChartAreas.Add(chartArea_YourData);
chart_MyChart.ChartAreas.Add(chartArea_OtherData);
/* Chart Area: My Data */
Series series01 = this.chart_MyChart.Series.Add("My first series");
series01.ChartArea = chartArea_MyData.Name;
Series series02 = this.chart_MyChart.Series.Add("My second series");
series02.ChartArea = chartArea_MyData.Name;
Legend legend01 = new Legend(series01.Name);
Legend legend02 = new Legend(series02.Name);
legend01.DockedToChartArea = chartArea_MyData.Name;
legend02.DockedToChartArea = chartArea_MyData.Name;
chart_MyChart.Legends.Add(legend01);
chart_MyChart.Legends.Add(legend02);
/* Chart Area: Your Data */
Series series03 = this.chart_MyChart.Series.Add("Your first series");
series03.ChartArea = chartArea_YourData.Name;
Series series04 = this.chart_MyChart.Series.Add("Your second series");
series04.ChartArea = chartArea_YourData.Name;
Legend legend03 = new Legend(series03.Name);
Legend legend04 = new Legend(series04.Name);
legend03.DockedToChartArea = chartArea_YourData.Name;
legend04.DockedToChartArea = chartArea_YourData.Name;
chart_MyChart.Legends.Add(legend03);
chart_MyChart.Legends.Add(legend04);
/* Chart Area: Other Data */
Series series05 = this.chart_MyChart.Series.Add("Other series");
series05.ChartArea = chartArea_OtherData.Name;
Legend legend05 = new Legend(series05.Name);
legend05.DockedToChartArea = chartArea_OtherData.Name;
chart_MyChart.Legends.Add(legend05);
foreach(Legend legend in chart_MyChart.Legends)
{
legend.IsDockedInsideChartArea = true;
}
foreach(widget myWidget in some.widget)
{
series01.Points.AddXY(widget.timeStamp, widget.data1);
series02.Points.AddXY(widget.timeStamp, widget.data2);
series03.Points.AddXY(widget.timeStamp, widget.data3);
series04.Points.AddXY(widget.timeStamp, widget.data4);
series05.Points.AddXY(widget.timeStamp, widget.data5);
}

At first glance you seem to be missing the association between the series and the Legend this is from the WebSamples project that you can download here, it really helps to see a full source examples.
In the MultiLegends section, the code looks like this:
// Add a second legend
Legend secondLegend = new Legend("Second");
secondLegend.BackColor = Color.FromArgb(((System.Byte)(220)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
secondLegend.BorderColor = Color.Gray;
secondLegend.Font = this.Chart1.Legends["Default"].Font;
this.Chart1.Legends.Add(secondLegend);
// Associate Series 2 with the second legend
this.Chart1.Series["Series 2"].Legend = "Second";
The only thing that I didn't see in your code is that last line of association.

Related

Chart axes in PowerPoint file

I'm creating a chart (of ComboChart type) in the PPTX file using GemBox.Presentation (together with GemBox.Spreadsheet). I'm using the code from the PowerPoint Chart example and added parts of the Excel Combo Chart example.
This is what I have so far:
var presentation = new PresentationDocument();
var slide = presentation.Slides.AddNew(SlideLayoutType.Custom);
var chart = slide.Content.AddChart(GemBox.Presentation.ChartType.Combo, 25, 25, 300, 500);
var comboChart = (ComboChart)chart.ExcelChart;
var worksheet = comboChart.Worksheet;
worksheet.Cells["A1"].Value = "Name";
worksheet.Cells["A2"].Value = "John Doe";
worksheet.Cells["A3"].Value = "Fred Nurk";
worksheet.Cells["B1"].Value = "Salary";
worksheet.Cells["B2"].Value = 4023;
worksheet.Cells["B3"].Value = 3263;
worksheet.Cells["C1"].Value = "Max";
worksheet.Cells["C2"].Value = 4500;
worksheet.Cells["C3"].Value = 4300;
worksheet.Cells["D1"].Value = "Min";
worksheet.Cells["D2"].Value = 3000;
worksheet.Cells["D3"].Value = 2800;
comboChart.CategoryLabelsReference = "A2:A3";
var salaryChart = comboChart.Add(GemBox.Spreadsheet.Charts.ChartType.Column);
salaryChart.Series.Add("=B1", "B2:B3");
var minMaxChart = comboChart.Add(GemBox.Spreadsheet.Charts.ChartType.Line);
minMaxChart.Series.Add("=C1", "C2:C3");
minMaxChart.Series.Add("=D1", "D2:D3");
presentation.Save("output.pptx");
And this is what I get:
Now my problem is that I cannot find any way to access and format the Category axis and Vertical axis.
I tried to use chart, comboChart, salaryChart, and minMaxChart objects, but none of them have any axes properties!?
How can I, let's say, set the axes titles?
To set the axes of the Combo chart, you'll need to use the axes of one of its containing charts, so either salaryChart or minMaxChart.
Now the reason why you don't see any axes properties on them is that they are of a base type (ExcelChart). You need to cast them to a derived type, like this:
var salaryChart = (ColumnChart)comboChart.Add(GemBox.Spreadsheet.Charts.ChartType.Column);
salaryChart.Series.Add("=B1", "B2:B3");
salaryChart.Axes.Horizontal.Title.Text = "My Categories";
salaryChart.Axes.Vertical.Title.Text = "My Values";

How can I remove line shadows on Live Chart line chart?

I created a line chart using a live chart. But I cannot eliminate the shadows (shaded areas in the image) under these lines.
LineSeries yeni = new LineSeries();
yeni.PointGeometrySize = 20;
yeni.LineSmoothness = 0;
// yeni.BitmapEffect.
// yeni.PointForeground = System.Windows.Media.Brushes.Transparent;
yeni.Title = kisiler[i].ToString();
yeni.Values = new ChartValues<double>(allValues);
cartesianChart1.Series.Add(yeni);
This way I solved the problem:
yeni.Fill = System.Windows.Media.Brushes.Transparent;

How to add tooltips to a Box plot in TeeChart?

As the title says, I am using TeeChart to draw several boxplots within one chart object. Since, the number of boxplots can be quite big, I want to be able to click on a box and have information pertaining to that series to show up as a tooltip.
I am currently trying to do this with a MarksTip but for some reason, when I try to hover over the box, MarksTip will sometimes open and then immediately close (basically being visible for a split second). I have already tried setting the hide delay but it seems to be ignoring that.
Code snippet below:
seriesIndex = 0;
foreach (var seriesData in seriesDataList)
{
var series = new Box()
series.UseCustomValues = true;
series.Box.HorizSize = 5;
series.Box.Style = PointerStyles.Rectangle;
series.MildOut.Visible = true;
series.MildOut.HorizSize = 2;
series.MildOut.VertSize = 2;
series.ExtrOut.Visible = true;
series.ExtrOut.HorizSize = 2;
series.ExtrOut.VertSize = 2;
series.LinePen.Visible = _isLineVisible;
series.Pointer.Pen.Visible = true;
series.ShowInLegend = false;
series.Add(seriesIndex, seriesData);
series.Title = "tooltip text";
var tooltip = new MarksTip(Chart.Chart)
{
Series = series,
Style = MarksStyles.SeriesTitle,
HideDelay = 31000,
};
Chart.Series.Add(series);
seriesIndex++;
}
Credit to this other question for pointing me in the right direction.
I ended up using the GetSeriesMark event to modify the text of a single chart-bound MarksTip instead of creating multiple series-bound `MarksTip
seriesIndex = 0;
Chart.Tools.Add(new MarksTip());
foreach (var seriesData in seriesDataList)
{
var series = new Box()
series.UseCustomValues = true;
//Other series appearance stuff
series.Add(seriesIndex, seriesData);
series.Title = "tooltip text";
series.GetSeriesMark += (s, args) =>
{
args.MarkText = s.Title;
};
series.Marks.Visible = false;
Chart.Series.Add(series);
seriesIndex++;
}
One note about this method. The tooltip will only appear when hovering over actual datapoints and not the whole box. Not ideal but at least I can read the tooltip now.

Dynamically creating charts

I am trying to dynamically create a chart for each drive in the computer, inside a form.
Each chart should be a pie chart that contains the amount of free space (colored green) and used space(colored red) in GBs.
But when I run the following code the only thing I see is blank rectangles with the titles of "C:\", "D:\" and so on.
Here is the code :
public static void DrawCharts()
{
Chart[] charts = new Chart[DriveInfo.GetDrives().Length];
DriveInfo[] drives = DriveInfo.GetDrives();
for (int i = 0; i < drives.Length; i++)
{
charts[i] = new Chart();
charts[i].Palette = ChartColorPalette.BrightPastel;
charts[i].Titles.Add(drives[i].Name);
charts[i].Series.Add("Storage");
charts[i].Series[0].ChartType = SeriesChartType.Pie;
charts[i].Location = new System.Drawing.Point(20 + i * 231, 30);
charts[i].Size = new System.Drawing.Size(230, 300);
DataPoint d = new DataPoint();
d.XValue = 1;
double[] p = { (double)drives[i].TotalFreeSpace / 1000000000 };
d.YValues = p;
d.Color = System.Drawing.Color.YellowGreen;
d.Label = "Free Space";
charts[i].Series[0].Points.Add(d);
d.Label = "Used Space";
d.XValue = 2;
double[] a = { (double)((drives[i].TotalSize - drives[i].TotalFreeSpace) / 1000000000) };
d.YValues = a;
d.Color = System.Drawing.Color.Red;
charts[i].Series[0].Points.Add(d);
Form1.tabs.TabPages[1].Controls.Add(charts[i]);
charts[i].Invalidate();
}
}
Thanks.
You are almost there.
But the most basic thing you need to add to a dynamically created chart..:
charts[i] = new Chart();
..is a ChartArea:
charts[i].ChartAreas.Add("CA1"); // pick your name!
Without it no Series can display..
Use it to style the axis with TickMarks, GridLines or Labels or to set Minima and Maxima and Intervals. Well, at least for most other ChartTypes; Pies don't need any of this anyway..
Note that you can have several ChartAreas in one Chart.
Also note that it still will display nothing until at least one Series has at least one DataPoint..

C# Winchart configuration, adding more lines

I am having some trouble with my C# chart.
I want to create a winnings chart.
This chart is what I want to create:
I currently have this:
WinChart.ChartAreas[0].AxisY.Title = "$ USD";
WinChart.ChartAreas[0].AxisY.Minimum = -1;
WinChart.ChartAreas[0].AxisY.Maximum = 1;
WinChart.ChartAreas[0].AxisX.Title = "Tourneys";
WinChart.ChartAreas[0].AxisX.Minimum = 0;
WinChart.ChartAreas[0].AxisX.IsStartedFromZero = true;
WinChart.Series[0].Points.Add(0);
WinChart.Series[0].Points.Add(0.10);
WinChart.Series[0].Points.Add(0.20);
WinChart.Series[0].Points.Add(0.30);
WinChart.Series[0].Points.Add(-0.50);
WinChart.Series[0].Points.Add(-0.60);
WinChart.Series[0].Points.Add(-0.70);
How can I make it start at the coordinates 0,0 and how do I make a middle line that is $0 ?
Documentation link: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.aspx
I'm going to assume some thing. WinChart is probably inheriting from Chart:
public class WinChart : Chart { }
and that ChartAreas is a ChartAreaCollection object and the same with Series
So I would do something like:
WinChart.ChartAreas[0].AxisY.Title = "$ USD";
WinChart.ChartAreas[0].AxisY.Minimum = -1;
WinChart.ChartAreas[0].AxisY.Maximum = 1;
WinChart.ChartAreas[0].AxisY.Interval = 0.2; // adjusts y axis scale
WinChart.ChartAreas[0].AxisX.Title = "Tourneys";
WinChart.ChartAreas[0].AxisX.Minimum = 0;
Series series = new Series();
series.Points.Add(0,0);
...
series.Points.Add(5, 1.05);
WinChart.Series.Add(series);
//repeat last five lines to add second line to graph

Categories

Resources