Chart Display after plot error - c#

I have a program to plot chart but now there is a problem where sometimes the chart does not show fully. Is there any way to fix it?
What I expect it to be:
Currently, the problem faced:
code, where i key, generate the graph
dataGridView1.Rows[8].Cells[0].Value.ToString(); // Start of X-T/B
float x1 = float.Parse(dataGridView1.Rows[8].Cells[0].Value.ToString()); // Get of X-T/B Value
var Chart = this.chart1.ChartAreas[0];
Chart.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
Chart.AxisY.Title = "Average";
var Chart1 = chart2.ChartAreas[0];
Chart1.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
Chart1.AxisY.Title = "Range";
Chart.AxisX.LabelStyle.Format = "";
Chart.AxisY.LabelStyle.Format = "";
Chart.AxisY.LabelStyle.IsEndLabelVisible = true;
Chart1.AxisX.LabelStyle.Format = "";
Chart1.AxisY.LabelStyle.Format = "";
Chart1.AxisY.LabelStyle.IsEndLabelVisible = true;
this.chart1.Series[0].XValueMember = dataGridView1.Columns[0].DataPropertyName;
this.chart1.Series[0].YValueMembers = dataGridView1.Columns[1].DataPropertyName;
this.chart1.DataSource = dataGridView1.DataSource;
chart2.Series[0].XValueMember = dataGridView1.Columns[0].DataPropertyName;
chart2.Series[0].YValueMembers = dataGridView1.Columns[1].DataPropertyName;
chart2.DataSource = dataGridView1.DataSource
double lclx = (Convert.ToDouble(label22.Text));
double uclx = (Convert.ToDouble(label21.Text));
double lclR = (Convert.ToDouble(label25.Text)); Caculation
double uclR = (Convert.ToDouble(label24.Text));Caculation
double y = lclx - 0.02;Caculation
double z = uclx + 0.02;Caculation
double p = lclR - 0.04;Caculation
if (lclR <= 0.00)Caculation
{
p = 0.00;Caculation
}
double b = uclR + 0.04;
Chart.AxisX.Minimum = 1; // Descriptions
Chart.AxisX.Maximum = 31; // Descriptions
Chart.AxisY.Minimum = y; // Descriptions
Chart.AxisY.Maximum = z; // Descriptions
Chart.AxisX.Interval = 1; // Descriptions
Chart.AxisY.Interval = 0.02; // Descriptions
Chart1.AxisX.Minimum = 1; // Descriptions
Chart1.AxisX.Maximum = 31; // Descriptions
Chart1.AxisY.Minimum = p; // Descriptions
Chart1.AxisY.Maximum = b; // Descriptions
Chart1.AxisX.Interval = 1; // Descriptions
Chart1.AxisY.Interval = 0.05;
this.chart1.Series.Clear();
this.chart1.Series.Dispose();
chart2.Series.Clear();
chart2.Series.Dispose();
this.chart1.Series.Add("X-T/B");
this.chart1.Series["X-T/B"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
this.chart1.Series["X-T/B"].Color = Color.Black;
this.chart1.Series["X-T/B"].BorderWidth = 3;
this.chart1.Series[0].IsVisibleInLegend = false;
chart2.Series.Add("Range");
chart2.Series["Range"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart2.Series["Range"].Color = Color.Black;
chart2.Series["Range"].BorderWidth = 3;
chart2.Series[0].IsVisibleInLegend = false;
this.chart1.Series["X-T/B"].MarkerStyle = MarkerStyle.Diamond;
this.chart1.Series["X-T/B"].MarkerSize = 10;
this.chart1.Series["X-T/B"].MarkerColor = Color.Blue;
chart2.Series["Range"].MarkerStyle = MarkerStyle.Diamond;
chart2.Series["Range"].MarkerSize = 10;
chart2.Series["Range"].MarkerColor = Color.Blue;
this.chart1.Series["X-T/B"].Points.AddXY(1, x1);

Related

C# EPPlus how to generate line chart with dash line

For last 2 days I stuck with a line chart issue with EPPlus. My objective is to create a line chart with EPPlus and line style will be dash line like below image. picture attached.
I search a lot Google for 2 days still got no relevant hint to have dash line in chart. I am not sure that at all EPPlus support dash line. I am using EPPlus version 6.0 and .NET Framework version 4.8. This is a sample code which generate chart in excel sheet with one line but I need that line will be dash line. please someone see my code and tell me what is missing in my code for which I could not have dash line style of line chart.
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var newFile = new FileInfo(filepath);
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("test");
worksheet.Cells["A1"].Value = 1;
worksheet.Cells["A2"].Value = 2;
worksheet.Cells["A3"].Value = 3;
worksheet.Cells["A4"].Value = 4;
worksheet.Cells["A5"].Value = 5;
worksheet.Cells["A6"].Value = 6;
worksheet.Cells["B1"].Value = 10000;
worksheet.Cells["B2"].Value = 10100;
worksheet.Cells["B3"].Value = 10200;
worksheet.Cells["B4"].Value = 10150;
worksheet.Cells["B5"].Value = 10250;
worksheet.Cells["B6"].Value = 10200;
//ExcelChart chart = worksheet.Drawings.AddChart("LineChart", eChartType.XYScatterSmooth);
ExcelChart chart = worksheet.Drawings.AddChart("LineChart", eChartType.XYScatterSmoothNoMarkers);
chart.Series.Add(ExcelRange.GetAddress(1, 2, worksheet.Dimension.End.Row, 2),
ExcelRange.GetAddress(1, 1, worksheet.Dimension.End.Row, 1));
var Series = chart.Series[0];
//chart.Axis[0].MinorGridlines.Fill.Color = Color.Red;
//chart.Axis[0].MinorGridlines.LineStyle = eLineStyle.LongDashDot;
chart.Axis[0].RemoveGridlines();
chart.Axis[1].RemoveGridlines();
chart.Axis[0].Border.LineStyle = eLineStyle.SystemDash;
//chart.XAxis.Border.LineStyle = eLineStyle.Dash;
chart.Series[0].Header = "Blah";
//chart.Series[0].Border.LineStyle = eLineStyle.DashDot;
//chart.Axis[0].Border.LineStyle = eLineStyle.Dash;
xlPackage.Save();
MessageBox.Show("Done");
}
I also check this post but could not implement it in my code Office Open XML: Drawing Dashed Line in Chart
Please push me to right direction to achieve my goal. Thanks in advance.
This works for me with same versions as you.
You need to add a reference to System.Drawing for the color:
// ...(code above this line did not change)
ExcelChart chart = worksheet.Drawings.AddChart("LineChart", eChartType.XYScatterSmoothNoMarkers);
chart.Series.Add(
ExcelRange.GetAddress(1, 2, worksheet.Dimension.End.Row, 2),
ExcelRange.GetAddress(1, 1, worksheet.Dimension.End.Row, 1));
chart.Series[0].Header = "Blah";
// Add this:
chart.Series[0].Border.LineStyle = OfficeOpenXml.Drawing.eLineStyle.Dash;
chart.Series[0].Border.Width = 2;
chart.Series[0].Border.Fill.Color = System.Drawing.Color.Blue;
chart.Axis[0].RemoveGridlines();
chart.Axis[1].RemoveGridlines();
xlPackage.Save();
Result:
#region chart summary 4
ExcelWorksheet Worksheet4 = p.Workbook.Worksheets.Add("Graph(4)");
var summary_Chart_four = Worksheet4.Drawings.AddChart("chart", eChartType.LineMarkers);
// var summary_Chart_four = Worksheet4.Drawings.AddChart("chart", eChartType.XYScatterLines) as ExcelScatterChart;
//var summary_Chart_four = Worksheet4.Drawings.AddChart("chart", (eChartType)System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash);
//var summary_Chart_four = myWorksheet.Drawings.AddChart("chart", (eChartType)System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash);
summary_Chart_four.ShowDataLabelsOverMaximum = true;
Worksheet4.Cells.Style.Font.Name = "Arial";
Worksheet4.Cells.Style.Font.Size = 10;
Worksheet4.Cells.Style.Font.Bold = false;
loopCnt = 0;
for (int i = 0; i < (cVM.PeerGroups.Count + cVM.IndexGroups.Count) * 4; i += 4)
{
int rowIndex = 10 + i;
int endRowIndex = 14 + i;
string startingColumn_series = "C" + rowIndex;
string endColumn_series = column + rowIndex;
string lastColumn_xaxis = column + 6;
int headerRowIndex = 9 + i;
string seriesHeader = "A" + headerRowIndex;
var series_i = summary_Chart_four.Series.Add(myWorksheet_summary_data.Cells[startingColumn_series + ":" + endColumn_series], myWorksheet_summary_data.Cells["C6:" + lastColumn_xaxis]);//working
series_i.Header = myWorksheet_summary_data.Cells[seriesHeader].Value.ToString();
summary_Chart_four.YAxis.Font.Size = 10;
summary_Chart_four.XAxis.Font.Size = 10;
//summary_Chart_four.XAxis.Format =DateTime.Now.Year.ToString();
summary_Chart_four.YAxis.Format = "$0";
if (loopCnt == 0)
{
string LineColor = "#000080";
series_i.Border.LineStyle = eLineStyle.Solid;
//series_i.LineColor = LineColor.ConvertToColor();
//series_i.LineWidth = 2.0;
//series_i.Marker.Style = eMarkerStyle.Diamond;
//series_i.Marker.Size = 8;
//string BGColor = "#99ccff";
//series_i.Marker.Fill.Color = BGColor.ConvertToColor();
//string strBorderColor = "#000080";
//series_i.Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Diamond;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 8;
string BGColor = "#99ccff";
//((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
string strBorderColor = "#000080";
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;
}
else if (loopCnt == 1)
{
string LineColor = "#003300";
series_i.Border.LineStyle = eLineStyle.LongDash;
//series_i.LineColor = LineColor.ConvertToColor();
//series_i.LineWidth = 2.0;
//series_i.Marker.Style = eMarkerStyle.Square;
////((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dash;
//series_i.Marker.Size = 7;
string BGColor = "#c0c0c0";
//series_i.Marker.Fill.Color = BGColor.ConvertToColor();
//string strBorderColor = "#003300";
//series_i.Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Square;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 7;
//((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
string strBorderColor = "#003300";
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;
}
else if (loopCnt == 2)
{
string LineColor = "#F79646";
series_i.Border.LineStyle = eLineStyle.Dot;
////series_i.LineColor = LineColor.ConvertToColor();
////series_i.LineWidth = 2.0;
////series_i.Marker.Style = eMarkerStyle.Circle;
//////((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dot;
////series_i.Marker.Size = 8;
string BGColor = "#ccffcc";
////series_i.Marker.Fill.Color = BGColor.ConvertToColor();
string strBorderColor = "#F79646";
////series_i.Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Circle;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 8;
//((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;
}
else if (loopCnt == 3)
{
string LineColor = "#c0504d";
series_i.Border.LineStyle = eLineStyle.LongDashDotDot;
////series_i.LineColor = LineColor.ConvertToColor();
////series_i.LineWidth = 2.0;
////series_i.Marker.Style = eMarkerStyle.Triangle;
//////((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dot;
////series_i.Marker.Size = 8;
string BGColor = "#c0504d";
////series_i.Marker.Fill.Color = BGColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Triangle;
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 8;
//((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
string strBorderColor = "#003300";
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;
}
//else if (loopCnt == 4)
//{
// string LineColor = "#c0504d";
// series_i.Border.LineStyle = eLineStyle.LongDashDotDot;
// series_i.LineColor = LineColor.ConvertToColor();
// series_i.LineWidth = 1.5;
// series_i.Marker.Style = eMarkerStyle.Square;
// //((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dot;
// series_i.Marker.Size = 8;
// string BGColor = "#c0504d";
// series_i.Marker.Fill.Color = BGColor.ConvertToColor();
//}
else
{
////series_i.LineColor = System.Drawing.Color.Aqua;
////series_i.LineWidth = 1.5;
////series_i.Marker.Style = eMarkerStyle.Circle;
////series_i.Marker.Size = 8;
////series_i.Marker.Fill.Color = System.Drawing.Color.Aqua;
}
//series_i.Smooth = eLineStyle.Dash;
//((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Series=eChartType.d
loopCnt++;
}
//var series_four = summary_Chart_four.Series.Add(myWorksheet_summary_data.Cells["C10:" + lastColumn_series1], myWorksheet_summary_data.Cells["C6:" + lastColumn_xaxis]);//working
//series_four.Header = myWorksheet_summary_data.Cells["A9"].Value.ToString();
//var series_four1 = summary_Chart_four.Series.Add(myWorksheet_summary_data.Cells["C14:" + lastColumn_series2], myWorksheet_summary_data.Cells["C6:" + lastColumn_xaxis]);
//series_four1.Header = myWorksheet_summary_data.Cells["A13"].Value.ToString();
summary_Chart_four.Border.Fill.Color = System.Drawing.Color.Black;
//summary_Chart_four.Title.Text = "Comparison of " + cVM.YearsBack + " Year Cumulative Total Return" + Environment.NewLine + " Assumes Initial Investment of $100" + Environment.NewLine + strMonth + " " + strSelectedDt.Year;
summary_Chart_four.Title.Text = "COMPARISON OF CUMULATIVE TOTAL RETURN";
summary_Chart_four.SetSize(960, 600);
// Add to 6th row and to the 6th column
summary_Chart_four.Legend.Font.Size = 10;
((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).Legend.Font.SetFromFont("Calibri", 11, false, false, false, false);
((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).YAxis.Font.SetFromFont("Arial", 10, false, false, false, false);
((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).XAxis.Font.SetFromFont("Arial", 10, false, false, false, false);
((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).Title.Font.Bold = true;
((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).Title.Font.SetFromFont("Calibri", 16, false, false, false, false);
//summary_Chart_four.Legend.Border.Fill.Color = System.Drawing.Color.Black;
summary_Chart_four.SetPosition(0, 0, 0, 0);
summary_Chart_four.XAxis.MajorTickMark = eAxisTickMark.In;
//summary_Chart.XAxis.MajorTickMark = eAxisTickMark.In;
summary_Chart_four.XAxis.MinorTickMark = eAxisTickMark.None;
summary_Chart_four.YAxis.MinorTickMark = eAxisTickMark.None;
summary_Chart_four.Legend.Position = eLegendPosition.Top;
summary_Chart_four.YAxis.CrossBetween = eCrossBetween.MidCat;
summary_Chart_four.XAxis.MajorGridlines.CompoundLineStyle = eCompundLineStyle.Double;

Excel Graph Issue in C#

I want Vertical Axis like this
But I'm getting this
I'm using EPPlus 3.1 (http://epplus.codeplex.com/)
using (var package = new ExcelPackage())
{
var workbook = package.Workbook;
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.Cells["C1"].Value = "Widgets";
worksheet.Cells["C2"].Value = 20;
worksheet.Cells["C3"].Value = 5;
worksheet.Cells["C4"].Value = 30;
worksheet.Cells["C5"].Value = 32;
worksheet.Cells["C6"].Value = 17;
worksheet.Cells["D1"].Value = "Widgets2";
worksheet.Cells["D2"].Value = 1;
worksheet.Cells["D3"].Value = 2;
worksheet.Cells["D4"].Value = 3;
worksheet.Cells["D5"].Value = 4;
worksheet.Cells["D6"].Value = 5;
worksheet.Cells["B2"].Value = "Jan";
worksheet.Cells["B3"].Value = "Feb";
worksheet.Cells["B4"].Value = "Mar";
worksheet.Cells["B5"].Value = "Apr";
worksheet.Cells["B6"].Value = "May";
var chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);
var series = chart.Series.Add("C2:C6", "B2:B6");
series.HeaderAddress = new ExcelAddress("'Sheet1'!C1");
series = chart.Series.Add("D2:D6", "B2:B6");
series.HeaderAddress = new ExcelAddress("'Sheet1'!D1");
package.SaveAs(new System.IO.FileInfo(Server.MapPath("Upload/" + Guid.NewGuid().ToString() + ".xlsx")));
}
You need to turn off the minor tick marks on the value axis. In Excel VBA it's this, I'll let you convert it:
chart.Axes(xlValue).MinorTickMark = xlTickMarkNone

Multiple Plots with shared axes in Oxyplot

I want to add multiple plots with shared x-axis using OXYPLOT library. The example code is as follows and it sets 4 different y-axis sharing the same x-axis. However i can only plot data on the 1st x&y axis but not the others. Any kind of suggestion would be appreciated.
[Example("Untitled")]
public static PlotModel Untitled()
{
var plotModel1 = new PlotModel();
plotModel1.PlotMargins = new OxyThickness(40,20,40,30);
var linearAxis1 = new LinearAxis();
linearAxis1.EndPosition = 0.25;
linearAxis1.Maximum = 1;
linearAxis1.Minimum = -1;
linearAxis1.Title = "C1";
linearAxis1.Key= "C1";
plotModel1.Axes.Add(linearAxis1);
var linearAxis2 = new LinearAxis();
linearAxis2.EndPosition = 0.5;
linearAxis2.Maximum = 1;
linearAxis2.Minimum = -1;
linearAxis2.Position = AxisPosition.Right;
linearAxis2.StartPosition = 0.25;
linearAxis2.Title = "C2";
linearAxis2.Key= "C2";
plotModel1.Axes.Add(linearAxis2);
var linearAxis3 = new LinearAxis();
linearAxis3.EndPosition = 0.75;
linearAxis3.Maximum = 1;
linearAxis3.Minimum = -1;
linearAxis3.StartPosition = 0.5;
linearAxis3.Title = "C3";
linearAxis3.Key= "C3";
plotModel1.Axes.Add(linearAxis3);
var linearAxis4 = new LinearAxis();
linearAxis4.Maximum = 1;
linearAxis4.Minimum = -1;
linearAxis4.Position = AxisPosition.Right;
linearAxis4.StartPosition = 0.75;
linearAxis4.Title = "C4";
linearAxis1.Key= "C4";
plotModel1.Axes.Add(linearAxis4);
var linearAxis5 = new LinearAxis();
linearAxis5.Maximum = 100;
linearAxis5.Minimum = 0;
linearAxis5.Position = AxisPosition.Bottom;
linearAxis5.Title = "s";
linearAxis5.Key= "s";
plotModel1.Axes.Add(linearAxis5);
return plotModel1;
}
Assign the XAxisKey and YAxisKey propertiy to your serises.
PlotModel pm = new PlotModel();
OxyPlot.Series.FunctionSeries s1 = new FunctionSeries(Math.Sin, -10, 10, 0.1, "sin(x)");
s1.YAxisKey = "axesY2";
s1.XAxisKey = "axesX2";
pm.Series.Add(s1);
In your case, the key is "C1", "C2", and "C3", etc.
As said before, you have to use the YAxisKey and XAxisKey attributes combined with the StartPosition and EndPosition. The position is in percentage (from 0 to 1) so for example, if you want to divide equally the Y axes of graph, you can try code like this:
float percentage = 1f / NumberOfGraphs;
for (int i = 1; i <= NumberOfGraphs; i++) {
...
LinearAxis yAxes = new LinearAxis();
yAxes.Position = OxyPlot.Axes.AxisPosition.Left;
yAxes.StartPosition = (i - 1) * percentage;
yAxes.EndPosition = i * percentage;
yAxes.Key = "Y" + i;
...
LineSeries lineSerie = new LineSeries();
lineSerie.YAxisKey = "Y" + i;
...
yourPlotView.Model.Series.Add(lineSerie)
}

C# Zedgraph Change Line Dash Length

I'm trying to change the length of the Dashes on the Zed Graph line. I would like to have larger gaps between the solid lines.
example of my code
LineItem LineCurve = null
LineCurve = ZedGraphControl.GraphPane.AddCurve("line1",PairPointListData, Color, Symbol);
//now I want to change the dash settings
LineCurve.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
LineCurve.Line.StepType = StepType.ForwardStep;
LineCurve.Line.DashOn = 1.0f;//Not sure what this floating point does
LineCurve.Line.DashOff = 1.0f;//Also not sure
I have changed the values of the Dash On and Off, but I can't see anything noticeable.
Your dash style must be set to 'Custom'. See:
http://zedgraph.sourceforge.net/documentation/html/P_ZedGraph_LineBase_DashOff.htm
Here is some sample code:
double[] xvals = new double[100];
double[] yvals = new double[100];
for (double i = 0; i < xvals.Length; i++)
{
xvals[(int)i] = i / 10;
yvals[(int)i] = Math.Sin(i / 10);
}
var zgc = msGraphControl1.zedGraphControl1;
var lineItem = zgc.GraphPane.AddCurve("Custom", xvals, yvals, Color.Blue);
lineItem.Line.Style = DashStyle.Custom;
lineItem.Line.Width = 3;
lineItem.Line.DashOn = 5;
lineItem.Line.DashOff = 10;
//offset the next curve
for (int i = 0; i < xvals.Length; i++)
{
xvals[i] = xvals[i] + 0.5;
yvals[i] = yvals[i] + 0.05;
}
var lineItem2 = zgc.GraphPane.AddCurve("DashDotDot", xvals, yvals, Color.Red);
lineItem2.Line.Width = 3;
lineItem2.Line.Style = DashStyle.DashDotDot;
//offset the next curve
for (int i = 0; i < xvals.Length; i++)
{
xvals[i] = xvals[i] + 0.5;
yvals[i] = yvals[i] + 0.05;
}
var lineItem3 = zgc.GraphPane.AddCurve("Solid", xvals, yvals, Color.Black);
lineItem3.Line.Width = 3;
lineItem3.Line.Style = DashStyle.Solid;
zgc.AxisChange();
zgc.Refresh();

Can't add curve to the ZedGraph in C#

I try to add multiple curves and yaxises using ZedGraph. But I added points to the first curve succesfully after I tried to add the second curve. The first one values' disappear and
myCurve.Points.Count equals 0. For example, if I add 6 curves, only the sixth one has values others count =0. Also any of them show up on the graph. Here is the code:
colors = new Color[ff.documentColumnCount + 4];
zedGraphControl1.IsShowPointValues = true;
myPane = zedGraphControl1.GraphPane;
LineItem myCurve;
Color[] colors;
myPane.XAxis.Type = ZedGraph.AxisType.Date;
myPane.XAxis.Scale.Format = "HH:mm:ss";
myPane.XAxis.Scale.MajorUnit = DateUnit.Second;
zamanValue = new double[ff.tarihSaat.Length - 4]; // x axis time values. ff is another windows form name, no problem here.
for (int i = 0; i < ff.tarihSaat.Length - 4; i++)
{
zamanValue[i] = (double)new XDate(ff.tarihSaat[i].Year,
ff.tarihSaat[i].Month,
ff.tarihSaat[i].Day,
ff.tarihSaat[i].Hour,
ff.tarihSaat[i].Minute,
ff.tarihSaat[i].Second);
counter++;
}
yaxisArray = new YAxis[ff.documentColumnCount + 4]; // temp y axises
for (int k = 0; k < chckboxNumber; k++)
{
tempPointPairList.Clear();
tempPointPairList = createPairPointList(k); // Creates points, I see the correct values everytime, also no problem here.
minYvalues[k] = Findmin(tempPointPairList);
maxYvalues[k] = FindMax(tempPointPairList);
myCurve = myPane.AddCurve(ff.columnNames[k + 3], tempPointPairList, colors[k], SymbolType.None);
myCurve.Line.Width = 2.5f;
//myCurve.IsVisible = true;
myCurve.YAxisIndex = k;
myCurve.IsVisible = true;
if (k == 0)
{
myPane.YAxis.IsVisible = true;
myPane.YAxis.Scale.Max = 1;
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.MajorStep = (myPane.YAxis.Scale.Max - myPane.YAxis.Scale.Min) / 10;
myPane.YAxis.MajorGrid.IsVisible = true;
}
else
{
yaxisArray[k] = new YAxis(ff.columnNames[k + 3]);
//yaxisArray[k].Color = colors[k];
yaxisArray[k].IsVisible = false;
yaxisArray[k].Title.IsVisible = false;
myPane.YAxisList.Add(yaxisArray[k]);
if (minYvalues[k] == maxYvalues[k])
{
yaxisArray[k].Scale.Min = minYvalues[k] - 0.1;
yaxisArray[k].Scale.Max = maxYvalues[k] + 0.1;
}
else
{
yaxisArray[k].Scale.Min = minYvalues[k];
yaxisArray[k].Scale.Max = maxYvalues[k];
}
myPane.YAxisList.Add(yaxisArray[k]);
}
yAxisListIndexes[k] = myPane.YAxisList.Count-1;
minTextBoxes[k].Text = minYvalues[k].ToString();
maxTextBoxes[k].Text = maxYvalues[k].ToString();
durum[k].previousState = 1;
durum[k].currentState = 1;
chckBoxList[k].Checked = true;
myCurve.Clear();
}
myPane.XAxis.Scale.Min = zamanValue[0];
myPane.XAxis.Scale.Max = zamanValue[zamanValue.Length - 1];
//myPane.YAxisList[0].IsVisible = true;
zedGraphControl1.AxisChange();
zedGraphControl1.Invalidate();
zedGraphControl1.Refresh();
Where is the mistake?
You don't add curves to each other, you add them to myPane.CurveList so you have them in myPane.CurveList[0], myPane.CurveList[1] and so on, not in myCurve. myCurve serves as store for current curve you are working with. When you call
myCurve = myPane.AddCurve(ff.columnNames[k + 3], tempPointPairList, colors[k], SymbolType.None);
a brand new curve is created, added to myPane.CurveList and is written into myCurve variable. It has a fresh state as it's just created. You can access your previous curve(s) in myPane.CurveList.

Categories

Resources