C# WinForms MSChart (StackedBar - Gantt Chart) With Y Axis Date Restrictions - c#

UPDATE: From what I've seen, this has little to do with the date itself. It's simply a case of having a long bar on the chart, when focusing in on a short segment of it (Date/Time, Short value ranges, etc), then it stops coloring the bar. Still no idea why or how to fix it, other than turning a blind eye to it.
I've been attempting to solve a weird little error involving stacked charts and date restrictions. Say you set the Y Axis as Dates, the X Axis is projects, and you use the bars as project completion, project deadline and project overdue.
Now if you set the minimum and maximum dates shown on the Y Axis to a few days apart, on a project expected to last months, you will see the problem. The bar loses its color, though if you increase the gap between minimum and maximum then the problem goes away.
Below is a snippit of the code involved:
private void _Gantt_Load(object sender, EventArgs e)
{
chart1.MouseUp += new MouseEventHandler(MouseHandler);
_dtpGraphStart.ValueChanged += new EventHandler(DateTimeHandler);
_dtpGraphEnd.ValueChanged += new EventHandler(DateTimeHandler);
string pOneName = "Project 1";
string pTwoName = "Project 2";
DateTime pOneStart = new DateTime(2011, 01, 01, 0, 0, 0);
DateTime pTwoStart = new DateTime(2011, 02, 01, 12, 0, 0);
DateTime pOneEnd = new DateTime(2011, 01, 01, 15, 0, 0);
DateTime pTwoEnd = new DateTime(2011, 07, 01, 7, 0, 0);
double pOneTotal = (pOneEnd - pOneStart).TotalDays;
double pTwoTotal = (pTwoEnd - pTwoStart).TotalDays;
double pOnePercent = 75;
double pTwoPercent = 50;
double pOneComplete = (pOnePercent / 100.0f) * pOneTotal;
double pTwoComplete = (pTwoPercent / 100.0f) * pTwoTotal;
chart1.Series["StartSeries"].Points.AddXY(pOneName, pOneStart);
chart1.Series["StartSeries"].Points.AddXY(pTwoName, pTwoStart);
chart1.Series["ProjectDurationSeries"].Points.AddXY(pOneName, pOneComplete);
chart1.Series["ProjectDurationSeries"].Points.AddXY(pTwoName, pTwoComplete);
chart1.Series["ProjectDurationSeries"].Points[0].Tag = "TestOne";
chart1.Series["ProjectDurationSeries"].Points[1].Tag = "TestTwo";
chart1.Series["ProjectRemainingSeries"].Points.AddXY(pOneName, pOneTotal - pOneComplete);
chart1.Series["ProjectRemainingSeries"].Points.AddXY(pTwoName, pTwoTotal - pTwoComplete);
chart1.Series["ProjectRemainingSeries"].Points[0].Tag = "TestCompleteOne";
chart1.Series["ProjectRemainingSeries"].Points[1].Tag = "TestCompleteTwo";
chart1.ChartAreas[0].AxisY.Minimum = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 0, 0, 0).ToOADate();
chart1.ChartAreas[0].AxisY.Maximum = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59).ToOADate();
chart1.DataBind();
_dtpGraphEnd.MinDate = _dtpGraphStart.Value;
_dtpGraphStart.MaxDate = _dtpGraphEnd.Value;
}
private void DateTimeHandler(object sender, EventArgs e)
{
DateTimePicker dtp = (DateTimePicker)sender;
switch (dtp.Name)
{
case "_dtpGraphStart":
chart1.ChartAreas[0].AxisY.Minimum = new DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, 0, 0, 0).ToOADate();
_dtpGraphEnd.MinDate = dtp.Value;
break;
case "_dtpGraphEnd":
chart1.ChartAreas[0].AxisY.Maximum = new DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, 23, 59, 59).ToOADate();
_dtpGraphStart.MaxDate = dtp.Value;
break;
}
if ((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) < 3)
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "T";
if (((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) > 3)
&& ((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) < 30))
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "dd/MM";
if ((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) > 30)
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "MM/yyyy";
}
The above has Projects spanning from 01/01/2011 00:00:00 to 01/01/2011 15:00:00, and 01/02/2011 12:00:00 to 01/07/2011 07:00:00.
Initially the date restriction is on todays date, for no particular reason.
Heres an image displaying the issue. No other code has anything to do with what this graph displays, other than minor property changes, but for the sake of being thorough, I'll add those below from the designer file.
chartArea1.AxisX.MajorGrid.Enabled = false;
chartArea1.AxisY.InterlacedColor = System.Drawing.Color.Lime;
chartArea1.AxisY.LabelStyle.Format = "T";
chartArea1.AxisY.MajorGrid.Enabled = false;
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Location = new System.Drawing.Point(0, 0);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series1.Color = System.Drawing.Color.White;
series1.Name = "StartSeries";
series2.BorderColor = System.Drawing.Color.Black;
series2.BorderWidth = 2;
series2.ChartArea = "ChartArea1";
series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
series2.EmptyPointStyle.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
series2.Name = "ProjectDurationSeries";
series3.BackHatchStyle = System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
series3.BackSecondaryColor = System.Drawing.Color.White;
series3.BorderColor = System.Drawing.Color.Black;
series3.BorderWidth = 2;
series3.ChartArea = "ChartArea1";
series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series3.Color = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
series3.Name = "ProjectRemainingSeries";
series4.BackHatchStyle = System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
series4.BorderColor = System.Drawing.Color.Black;
series4.BorderWidth = 2;
series4.ChartArea = "ChartArea1";
series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series4.Color = System.Drawing.Color.Red;
series4.Name = "Series4";
this.chart1.Series.Add(series1);
this.chart1.Series.Add(series2);
this.chart1.Series.Add(series3);
this.chart1.Series.Add(series4);
this.chart1.Size = new System.Drawing.Size(727, 339);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";
Heres an image displaying the problem: http://i55.tinypic.com/27zkv0w.jpg

chartArea1.AxisX.MajorGrid.Enabled = false;
chartArea1.AxisY.InterlacedColor = System.Drawing.Color.Lime;
chartArea1.AxisY.LabelStyle.Format = "T";
chartArea1.AxisY.MajorGrid.Enabled = false;
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Location = new System.Drawing.Point(0, 0);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series1.Color = System.Drawing.Color.White;
series1.Name = "StartSeries";
series2.BorderColor = System.Drawing.Color.Black;
series2.BorderWidth = 2;
series2.ChartArea = "ChartArea1";
series2.ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)
(192)))), ((int)(((byte)(0)))));
series2.EmptyPointStyle.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))),
((int)(((byte)(192)))), ((int)(((byte)(0)))));
series2.Name = "ProjectDurationSeries";
series3.BackHatchStyle =
System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
series3.BackSecondaryColor = System.Drawing.Color.White;
series3.BorderColor = System.Drawing.Color.Black;
series3.BorderWidth = 2;
series3.ChartArea = "ChartArea1";
series3.ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series3.Color = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)
(((byte)(128)))), ((int)(((byte)(255)))));
series3.Name = "ProjectRemainingSeries";
series4.BackHatchStyle =
System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
series4.BorderColor = System.Drawing.Color.Black;
series4.BorderWidth = 2;
series4.ChartArea = "ChartArea1";
series4.ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series4.Color = System.Drawing.Color.Red;
series4.Name = "Series4";
this.chart1.Series.Add(series1);
this.chart1.Series.Add(series2);
this.chart1.Series.Add(series3);
this.chart1.Series.Add(series4);
this.chart1.Size = new System.Drawing.Size(727, 339);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";

Related

How to set chart title above y-axis?

I have been experimenting with charts and so far I got this experimental code:
static void Main(string[] args)
{
Random rnd = new Random();
Chart thisChart = new Chart();
thisChart.Height = 400;
thisChart.Width = 500;
thisChart.BackColor = SystemColors.Window;
thisChart.Palette = ChartColorPalette.EarthTones;
thisChart.Titles.Add("Test");
thisChart.Visible = true;
ChartArea ca = new ChartArea();
ca.Name = "Default";
ca.BackColor = Color.White;
ca.BorderColor = Color.FromArgb(26, 59, 105);
ca.BorderWidth = 0;
ca.BorderDashStyle = ChartDashStyle.Solid;
ca.AxisX = new Axis();
ca.AxisY = new Axis();
thisChart.ChartAreas.Add(ca);
Series series = thisChart.Series.Add("Default");
series.ChartType = SeriesChartType.Spline;
series.ChartArea = "Default";
series.Points.AddXY("1", 20);
series.Points.AddXY("2", 25);
series.Points.AddXY("3", 30);
series.Points.AddXY("4", 35);
series.Points.AddXY("5", 40);
series.Points.AddXY("6", 45);
//SetPosition for multiple Y-axis labels
thisChart.ChartAreas["Default"].Position = new ElementPosition(25, 10, 68, 85);
thisChart.ChartAreas["Default"].InnerPlotPosition = new ElementPosition(10, 0, 90, 80);
// Create extra Y axis for second and third series
Series series2 = thisChart.Series.Add("Series2");
series2.ChartType = SeriesChartType.Spline;
series2.ChartArea = "Default";
series2.Points.AddXY("1", 50);
series2.Points.AddXY("2", 55);
series2.Points.AddXY("3", 60);
series2.Points.AddXY("4", 70);
series2.Points.AddXY("5", 75);
series2.Points.AddXY("6", 80);
//series2.Legend = "Default";
thisChart.Series["Series2"].ChartArea = "Default";
CreateYAxis(thisChart, thisChart.ChartAreas["Default"], thisChart.Series["Series2"], 13, 8);
// Create extra X axis for second and third series
Series series3 = thisChart.Series.Add("Series3");
series3.ChartType = SeriesChartType.Spline;
series3.ChartArea = "Default";
series3.Points.AddXY("1,5", 75);
series3.Points.AddXY("2,5", 175);
series3.Points.AddXY("3,5", 300);
series3.Points.AddXY("4,5", 75);
series3.Points.AddXY("5,5", 150);
series3.Points.AddXY("6,6", 125);
thisChart.Series["Series3"].ChartArea = "Default";
CreateXAxis(thisChart, thisChart.ChartAreas["Default"], thisChart.Series["Series3"], -10, 8);
//thisChart.DataBind();
thisChart.SaveImage(#"C:\Temp\TestChart.png", ChartImageFormat.Png);
}
private static void CreateYAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
{
// Create new chart area for original series
ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
areaSeries.BackColor = Color.Transparent;
areaSeries.BorderColor = Color.Transparent;
areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
areaSeries.AxisX.MajorGrid.Enabled = false;
areaSeries.AxisX.MajorTickMark.Enabled = false;
areaSeries.AxisX.LabelStyle.Enabled = false;
areaSeries.AxisY.MajorGrid.Enabled = false;
areaSeries.AxisY.MajorTickMark.Enabled = false;
areaSeries.AxisY.LabelStyle.Enabled = false;
areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
areaSeries.AxisY.TitleAlignment = StringAlignment.Far;
areaSeries.AxisY.TextOrientation = TextOrientation.Horizontal;
areaSeries.AxisY.Title = "Y-axis Title1";
areaSeries.AxisY.TitleForeColor = Color.Blue;
areaSeries.AxisY.TitleFont = new Font("Tahoma", 7, FontStyle.Bold);
areaSeries.AxisY.Maximum = 300;
series.ChartArea = areaSeries.Name;
// Create new chart area for axis
ChartArea areaAxis = chart.ChartAreas.Add("AxisY_" + series.ChartArea);
areaAxis.BackColor = Color.Transparent;
areaAxis.BorderColor = Color.Transparent;
areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());
areaAxis.AxisY.TitleAlignment = StringAlignment.Center;
areaAxis.AxisY.Title = "Y-axis Title";
areaAxis.AxisY.TitleForeColor = Color.Blue;
areaAxis.AxisY.TitleFont = new Font("Tahoma", 7, FontStyle.Bold);
areaAxis.AxisY.TitleAlignment = StringAlignment.Far;
areaAxis.AxisY.TextOrientation = TextOrientation.Horizontal;
areaAxis.AxisY.Maximum = 200;
// Create a copy of specified series
Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
seriesCopy.ChartType = series.ChartType;
foreach (DataPoint point in series.Points)
{
seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
}
// Hide copied series
seriesCopy.IsVisibleInLegend = false;
seriesCopy.Color = Color.Transparent;
seriesCopy.BorderColor = Color.Transparent;
seriesCopy.ChartArea = areaAxis.Name;
// Disable drid lines & tickmarks
areaAxis.AxisX.LineWidth = 0;
areaAxis.AxisX.MajorGrid.Enabled = false;
areaAxis.AxisX.MajorTickMark.Enabled = false;
areaAxis.AxisX.LabelStyle.Enabled = false;
areaAxis.AxisY.MajorGrid.Enabled = false;
areaAxis.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
areaAxis.AxisY.LabelStyle.Font = area.AxisY.LabelStyle.Font;
// Adjust area position
areaAxis.Position.X -= axisOffset;
areaAxis.InnerPlotPosition.X += labelsSize;
}
private static void CreateXAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
{
// Create new chart area for original series
ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
areaSeries.BackColor = Color.Transparent;
areaSeries.BorderColor = Color.Transparent;
areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
areaSeries.AxisY.MajorGrid.Enabled = false;
areaSeries.AxisY.MajorTickMark.Enabled = false;
areaSeries.AxisY.LabelStyle.Enabled = false;
areaSeries.AxisX.MajorGrid.Enabled = false;
areaSeries.AxisX.MajorTickMark.Enabled = false;
areaSeries.AxisX.LabelStyle.Enabled = false;
areaSeries.AxisX.IsStartedFromZero = area.AxisX.IsStartedFromZero;
series.ChartArea = areaSeries.Name;
// Create new chart area for axis
ChartArea areaAxis = chart.ChartAreas.Add("AxisX_" + series.ChartArea);
areaAxis.BackColor = Color.Transparent;
areaAxis.BorderColor = Color.Transparent;
areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());
// Create a copy of specified series
Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
seriesCopy.ChartType = series.ChartType;
foreach (DataPoint point in series.Points)
{
seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
}
// Hide copied series
seriesCopy.IsVisibleInLegend = false;
seriesCopy.Color = Color.Transparent;
seriesCopy.BorderColor = Color.Transparent;
seriesCopy.ChartArea = areaAxis.Name;
// Disable drid lines & tickmarks
areaAxis.AxisY.LineWidth = 0;
areaAxis.AxisY.MajorGrid.Enabled = false;
areaAxis.AxisY.MajorTickMark.Enabled = false;
areaAxis.AxisY.LabelStyle.Enabled = false;
areaAxis.AxisX.MajorGrid.Enabled = false;
//areaAxis.AxisX.IntervalOffset = Convert.ToDouble("0,5");
areaAxis.AxisX.IsStartedFromZero = area.AxisX.IsStartedFromZero;
areaAxis.AxisX.LabelStyle.Font = area.AxisX.LabelStyle.Font;
areaAxis.AxisX.CustomLabels.Add(0, 1, "0.5");
areaAxis.AxisX.CustomLabels.Add(1, 2, "1.5");
areaAxis.AxisX.CustomLabels.Add(2, 3, "2.5");
areaAxis.AxisX.CustomLabels.Add(3, 4, "3.5");
areaAxis.AxisX.CustomLabels.Add(4, 5, "4.5");
areaAxis.AxisX.CustomLabels.Add(5, 6, "5.5");
areaAxis.AxisX.CustomLabels.Add(6, 7, "6.5");
// Adjust area position
areaAxis.Position.Y -= axisOffset;
areaAxis.InnerPlotPosition.Y += labelsSize;
}
This produces the following result:
PROBLEM:
I can not figure out how to make it so that the title aligns itself above the respective y-axis, any ideas?
I don't think you do that.
Afaik the recommended workaround is adding more chart Titles.
You can style them as usual and to move them on top of a axis you can align them to the top left of the respective Chartearea.InnerPlotPosition:
ChartArea ca1 = thisChart.ChartAreas[0];
RectangleF rip1 = ca1.InnerPlotPosition.ToRectangleF();
Title ty1 = thisChart.Titles.Add("ty1");
ty1.Text = "Y-Axis 1\nTitle";
ty1.ForeColor = Color.DarkSlateBlue;
ty1.Position.X = rip1.Left;
ty1.Position.Y = rip1.Y;
Make sure to have enough space at the top of the chart for the titles..
Do note that the values of all ElementPositions, including InnerPlotPosition are in percent of the respective containers, i.e. of the ChartArea for the InnerPlotPosition and of the Chart for the ChartArea..

C# Windows Forms Chart is weird

I made a C# windows forms and my chart is very strange. My chart has a lot of white borders and the text for Master and Slave is very tiny and pixelated.
Why the Axis is so far away from the borders? How can I fix this?
// chart1
//
chartArea2.AxisX.MajorGrid.Enabled = false;
chartArea2.AxisX.MajorTickMark.Enabled = false;
chartArea2.AxisY.MajorGrid.Enabled = false;
chartArea2.AxisY.MajorTickMark.Enabled = false;
chartArea2.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea2);
legend2.Name = "Legend1";
this.chart1.Legends.Add(legend2);
this.chart1.Location = new System.Drawing.Point(543, 49);
this.chart1.Name = "chart1";
series3.ChartArea = "ChartArea1";
series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
series3.Legend = "Legend1";
series3.MarkerBorderColor = System.Drawing.Color.Black;
series3.MarkerImage = "C:\\Users\\Tiago\\Desktop\\CODIGO_TESE_FINAL_BACKUP1408_BOM\\C# - AR.Drone SDK\\AR.Dron" +
"e\\icone_drone_2_50x50.png";
series3.MarkerImageTransparentColor = System.Drawing.Color.Red;
series3.Name = "Master";
series4.ChartArea = "ChartArea1";
series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
series4.Legend = "Legend1";
series4.MarkerImage = "C:\\Users\\Tiago\\Desktop\\CODIGO_TESE_FINAL_BACKUP1408_BOM\\C# - AR.Drone SDK\\AR.Dron" +
"e\\icone_drone_2_50x50.png";
series4.Name = "Slave";
this.chart1.Series.Add(series3);
this.chart1.Series.Add(series4);
this.chart1.Size = new System.Drawing.Size(1159, 359);
this.chart1.TabIndex = 7;
this.chart1.Text = "chart1";
this.chart1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseDown);
this.chart1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseMove);
this.chart1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseUp);
You can set the border spaces and the Fonts all in the designer.
Pick the ChartArea and modify the Position. Note that it is not in pixels but in percentages of the Chart.ClientSize!! So for your largish chart the 3% defualt is a little too much..
The Legend's Font is changed here:

How To Customize MVC Chart Legend?

I have drawn an MVC chart.
Just wanted to know how to achieve circular series name.
Please help. My code is below:
public ActionResult CreateBar()
{
// Create OnPremise and Azure Output Models
var objOnPremiseOutPut = new OnPremisesOutputModel();
var objAzureOutPut = new AzureCostOutputModel();
// Get the data from the TempData. Check :: What if the TemData is empty ??
objOnPremiseOutPut = (OnPremisesOutputModel)TempData["TotalOnPremiseCost"];
objAzureOutPut = (AzureCostOutputModel)TempData["TotalAzureCost"];
// Create a new chart Object
var chart = new Chart();
// Set Dimensions of chart
chart.Width = 500;
chart.Height = 350;
// Set Color and Background color properties
chart.BackColor = Color.WhiteSmoke;//Color.FromArgb(255, 255, 255);
chart.BorderlineDashStyle = ChartDashStyle.Solid;
chart.BackSecondaryColor = Color.White;
//chart.BackGradientStyle = GradientStyle.TopBottom;
chart.BorderlineWidth = 0;
chart.Palette = ChartColorPalette.BrightPastel;
chart.BorderlineColor = Color.FromArgb(26, 59, 105);
chart.RenderType = RenderType.BinaryStreaming;
chart.BorderSkin.SkinStyle = BorderSkinStyle.None;
chart.AntiAliasing = AntiAliasingStyles.All;
chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;
// Set the Title and Legends for chart
chart.Titles.Add(CreateTitle());
chart.Legends.Add(new Legend("Costs Distribution")
{
BackColor = Color.Transparent,
Font = new Font("Trebuchet MS", 8.25f, FontStyle.Bold,
GraphicsUnit.Point),
IsTextAutoFit = false
});
// Clear any existing series and add the new series
chart.Series.Clear();
chart.Series.Add(new Series());
chart.Series.Add(new Series());
chart.Series.Add(new Series());
chart.Series.Add(new Series());
foreach (Series s in chart.Series)
{
s.ChartType = SeriesChartType.StackedColumn;
}
//var totalStorage = objAzureOutPut.GeoReplicatedStorageAnnualExpense+objAzureOutPut.LocallyRedundantStorageAnnualExpense
//chart.Series["Default"].Points[0].AxisLabel = "On Premises";
//chart.Series["Default"].Points[1].AxisLabel = "Azure";
chart.Series[0].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremServerExpense));
chart.Series[1].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremStorageCapacity));
chart.Series[2].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremNetworkExpense));
chart.Series[3].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremITExpense));
//chart.Series[0].AxisLabel = "Azure";
chart.Series[0].Points.Add(new DataPoint(1, objAzureOutPut.DiscVMPricingAnnualExpense));
chart.Series[1].Points.Add(new DataPoint(1, objAzureOutPut.TotalStorageExpense));
chart.Series[2].Points.Add(new DataPoint(1, objAzureOutPut.Zone1EgressAnnualExpense));
chart.Series[3].Points.Add(new DataPoint(1, objAzureOutPut.AdminExpense));
// Name the series
chart.Series["Series1"].Name = "Server";
chart.Series["Series2"].Name = "Storage";
chart.Series["Series3"].Name = "Network";
chart.Series["Series4"].Name = "IT";
chart.Series["IT"].MarkerStyle = MarkerStyle.Circle;
chart.Series["IT"].MarkerSize = 5;
// Create Memorysteam to dump the image
MemoryStream ms = new MemoryStream();
CreateChartArea(chart).SaveImage(ms);
return File(ms.GetBuffer(), #"image/png");
}
public Title CreateTitle()
{
Title title = new Title();
title.Text = "Cost Comparison";
title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
title.Font = new Font("Trebuchet MS", 14F, FontStyle.Bold);
title.ShadowOffset = 3;
title.ForeColor = Color.FromArgb(26, 59, 105);
return title;
}
public Chart CreateChartArea(Chart chart)
{
ChartArea chartArea = new ChartArea();
chartArea.Name = "Cost Comaparison";
chartArea.BackColor = Color.WhiteSmoke;
// X Axis interval
chartArea.AxisX.Interval = 1;
// Set the Custom Labebls
CustomLabel onPremXLabel = new CustomLabel(-0.5, 0.5, "On Premise", 0, LabelMarkStyle.None);
CustomLabel azureXLabel = new CustomLabel(0.75, 1.25, "Azure", 0, LabelMarkStyle.None);
chartArea.AxisX.CustomLabels.Add(onPremXLabel);
chartArea.AxisX.CustomLabels.Add(azureXLabel);
//chartArea.AxisY.Title = "Costs (in $)";
chartArea.AxisY.LabelStyle.Format = "C";
//chartArea.AxisY.TitleFont = new Font("Verdana,Arial,Helvetica,sans-serif",
// 12F, FontStyle.Bold);
//chartArea.AxisX.LabelStyle.Font =
new Font("Verdana,Arial,Helvetica,sans-serif",
8F, FontStyle.Regular);
chartArea.AxisY.LabelStyle.Font =
new Font("Verdana,Arial,Helvetica,sans-serif",
8F, FontStyle.Regular);
chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
chart.ChartAreas.Add(chartArea);
chart.ChartAreas["Cost Comaparison"].AxisY.MajorGrid.Enabled = false;
chart.ChartAreas["Cost Comaparison"].AxisX.MajorGrid.Enabled = false;
chartArea.AxisX.MinorTickMark.Enabled = false;
chartArea.AxisX.MajorTickMark.Enabled = false;
return chart;
}
Handle the CustomizeLegend event, as below:
private void Chart1_CustomizeLegend(object sender, CustomizeLegendEventArgs e)
{
foreach (LegendItem li in e.LegendItems)
{
li.ImageStyle = LegendImageStyle.Marker;
li.MarkerStyle = MarkerStyle.Circle;
li.MarkerSize = 72;
}
}
EDIT: In the Controller, after creating the chart, and before returning it to the View, register to handle the event, like this:
chart1.CustomizeLegend += Chart1_CustomizeLegend;

MS Chart: X-AXIS labels repeating

I am trying to create multi series line chart using MS Chart. I have created it successfully. But the problem is the x-axis labels repeation. Here is what is created
Can anyone tell me why the months are repeated? how can i avoid it?
UPDATE:
Here is the code:
DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
decimal[] gvals = {4.3,0};
decimal[] ypvals = {0,0};
decimal[] yvals = {3.5,0};
// create the chart
var chart = new Chart();
chart.Size = new Size(600, 250);
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
chart.BorderlineWidth = 3;
var chartArea = new ChartArea();
chartArea.AxisX.MajorGrid.LineWidth = 0;
//Remove Y-axis grid lines
chartArea.AxisY.MajorGrid.LineWidth = 0;
chartArea.AxisX.LabelStyle.Format = "MMM";
chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chart.ChartAreas.Add(chartArea);
var series = new Series();
series.Name = "Y";
series.Legend = "Y";
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.DateTime;
series.IsVisibleInLegend = true;
series.Color = Color.Red;
series.IsValueShownAsLabel = true;
series.BorderWidth = 2;
chart.Series.Add(series);
// bind the datapoints
chart.Series[0].Points.DataBindXY(xvals, yvals);
var series2 = new Series();
series2.Name = "YP";
series2.Legend = "YP";
series2.ChartType = SeriesChartType.Line;
series2.XValueType = ChartValueType.DateTime;
series2.IsVisibleInLegend = true;
series2.Color = Color.Yellow;
series2.IsValueShownAsLabel = true;
series2.BorderWidth = 2;
chart.Series.Add(series2);
// bind the datapoints
chart.Series[1].Points.DataBindXY(xvals, ypvals);
var series3 = new Series();
series3.Name = "G";
series3.Legend = "GG";
series3.ChartType = SeriesChartType.Line;
series3.XValueType = ChartValueType.DateTime;
series3.IsVisibleInLegend = true;
series3.Color = Color.Blue;
series3.IsValueShownAsLabel = true;
series3.BorderWidth = 2;
chart.Series.Add(series3);
// bind the datapoints
chart.Series[2].Points.DataBindXY(xvals, gvals);
// draw!
chart.Invalidate();
// write out a file
chart.SaveImage("D:\\cha.png", ChartImageFormat.Png);
Ok, I have got the solution.
I have just converted the
series1.XValueType = ChartValueType.Date;
series2.XValueType = ChartValueType.Date;
series3.XValueType = ChartValueType.Date;
TO:
series1.XValueType = ChartValueType.String;
series2.XValueType = ChartValueType.String;
series3.XValueType = ChartValueType.String;
and instead of using dates in the xAxisValues array used months name as string.
DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
TO:
string[] xvals = {DateTime.Now.AddMonth(-1).ToString("MMM"),DateTime.Now.ToString("MMM")};
Hope this helps someone else.

Chart messing zoom on ResetAutoValues

I´m using a chart to plot some infos from a Sensor. Everything is going right, until I reach the interval of 3660 points.
So, what I want to do is when my points reach that value, my chart will remove the first point and add the new one, take a look at the code:
if (graficoKVm.Series[0].Points.Count > 3660)
{
graficoKVm.Series[0].Points.RemoveAt(0);
graficoKVm.Series[0].Points.AddXY(DateTime.Now.ToOADate(), _sensorCampoEletroMagnetico.kVm);
graficoKVm.ResetAutoValues();
}
The problem is that, if the chart is zoomed, when it execute the graficoKVm.ResetAutoValues(); my zoom got messed up.
So far I´ve tried to store the zoom values and then apply the zoom after the reset, but I´ve failed implementing it. Here is the chart declaration:
this.graficoKVm.BackColor = System.Drawing.Color.Black;
this.graficoKVm.BorderSkin.BorderColor = System.Drawing.Color.White;
chartArea1.AxisX.Interval = 5D;
chartArea1.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.LabelStyle.Angle = 45;
chartArea1.AxisX.LabelStyle.ForeColor = System.Drawing.Color.White;
chartArea1.AxisX.LabelStyle.Format = "HH:mm";
chartArea1.AxisX.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisX.MajorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MinorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MinorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.ScaleBreakStyle.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.ScaleView.SizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.ScaleView.SmallScrollMinSize = 5D;
chartArea1.AxisX.ScaleView.SmallScrollMinSizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.ScaleView.SmallScrollSizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisX.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisX.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.AxisX2.LineColor = System.Drawing.Color.White;
chartArea1.AxisX2.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisX2.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisX2.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisX2.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.AxisY.Interval = 2D;
chartArea1.AxisY.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
chartArea1.AxisY.LabelStyle.ForeColor = System.Drawing.Color.White;
chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisY.MajorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.Maximum = 18D;
chartArea1.AxisY.Minimum = -18D;
chartArea1.AxisY.MinorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.MinorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.ScaleBreakStyle.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.ScaleView.SmallScrollMinSize = 0.5D;
chartArea1.AxisY.ScaleView.SmallScrollSize = 0.5D;
chartArea1.AxisY.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisY.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisY.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.AxisY2.LineColor = System.Drawing.Color.White;
chartArea1.AxisY2.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisY2.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisY2.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisY2.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.BackColor = System.Drawing.Color.Black;
chartArea1.BorderColor = System.Drawing.Color.White;
chartArea1.CursorX.Interval = 0.01D;
chartArea1.CursorX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.CursorX.IsUserEnabled = true;
chartArea1.CursorX.IsUserSelectionEnabled = true;
chartArea1.CursorX.SelectionColor = System.Drawing.Color.White;
chartArea1.CursorY.IsUserEnabled = true;
chartArea1.CursorY.IsUserSelectionEnabled = true;
chartArea1.CursorY.SelectionColor = System.Drawing.Color.White;
chartArea1.InnerPlotPosition.Auto = false;
chartArea1.InnerPlotPosition.Height = 91.06797F;
chartArea1.InnerPlotPosition.Width = 95F;
chartArea1.InnerPlotPosition.X = 5F;
chartArea1.InnerPlotPosition.Y = 2.16495F;
chartArea1.Name = "ChartArea1";
chartArea1.Position.Auto = false;
chartArea1.Position.Height = 97F;
chartArea1.Position.Width = 97F;
this.graficoKVm.ChartAreas.Add(chartArea1);
this.graficoKVm.Location = new System.Drawing.Point(3, 270);
this.graficoKVm.Margin = new System.Windows.Forms.Padding(0);
this.graficoKVm.Name = "graficoKVm";
series1.BorderWidth = 2;
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
series1.IsVisibleInLegend = false;
series1.Name = "Valor";
series1.ShadowColor = System.Drawing.Color.Black;
series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
this.graficoKVm.Series.Add(series1);
this.graficoKVm.Size = new System.Drawing.Size(1201, 295);
this.graficoKVm.TabIndex = 27;
this.graficoKVm.Text = "chart1";
Have you tried in your code to use Refresh or Update? When the index reaches 3660, the chart will be replotted with the Point saved in your series, no matter how much zoom you applied.
To make it work in the right way, I've done this implementation:
if (graficoKVm.Series[0].Points.Count > 3659)
{
graficoKVm.Series[0].Points.AddXY(DateTime.Now.ToOADate(), _sensorCampoEletroMagnetico.kVm);
if (!graficoKVm.ChartAreas[0].AxisX.ScaleView.IsZoomed &&
!graficoKVm.ChartAreas[0].AxisY.ScaleView.IsZoomed &&
!graficoKVm.ChartAreas[0].AxisY2.ScaleView.IsZoomed &&
!graficoKVm.ChartAreas[0].AxisX2.ScaleView.IsZoomed)
{
while (graficoKVm.Series[0].Points.Count > 3660)
{
graficoKVm.Series[0].Points.RemoveAt(0);
}
graficoKVm.ResetAutoValues();
}
}
So, if the chart is zoomed on any Axis, it will not reset the min and max value from each Axis. Eliminating the zoom problem.

Categories