C# EPPlus how to generate line chart with dash line - c#

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;

Related

How to create redar diagram in excel using Epplus in dotnet 6

I have created report in excel sheet which Is described in image1 and this image should give redar diagram which in in Image2 in different excel sheet like given in screenshot.
there are two excel sheet in one excel as you can see in screenshot so in chart sheet there should be redar diagram and sheet named sheet there is excel report valud as you can see in screenshot
I have already done piechart and excel report as well and I get stocked while making redar diagram ,
example of that I had made piechart and excel report :
public static class TestRunTestCaseExportHelper {
public static byte[] TestRunTestCaseByTestRunIdToExcel(List<TestRunTestCaseExportModel> data1, List<TestRunTestCaseExportTestResultCountModel> datas, List<FunctionModuleModel> funcitonData, TestRunTestCaseCountPercentageModel testRunStatusCountPercentage)
{
try
{
byte[] result;
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
int cellNumb = 1;
string[] totalColumns = {
"SN",
"Test Case",
"Test Plan",
"Status"
};
string[] totalColumn = {
"Test Plan",
"Passed",
"Failed",
"Pending",
"Blocked",
};
string[] totalColumn3 = {
"Function",
"Count"
};
string[] totalColumn4 = {
"Status",
"Percentage",
};
var worksheet = package.Workbook.Worksheets.Add("Chart");
var worksheets = package.Workbook.Worksheets.Add("sheet");
//worksheet.InsertRow(5, 2);
var rand = new Random();
var testRunStatusCountPercentages = testRunStatusCountPercentage;
var data = new List<KeyValuePair<string, int>>
{
new KeyValuePair<string, int>("Passed", testRunStatusCountPercentages.PassedPercentage),
new KeyValuePair<string, int>("Failed", testRunStatusCountPercentages.FailedPercentage),
new KeyValuePair<string, int>("Pending",85),
new KeyValuePair<string, int>("Blocked", 15),
};
for (var i = 0; i < totalColumn4.Length; i++)
{
worksheets.Cells[1, i + 16].Value = totalColumn4[i]; //populate header row
}
var m = 2;
//Fill the table
var startCell = worksheets.Cells[1, 16];
if (testRunStatusCountPercentage != null)
{
for (var i = 0; i < data.Count(); i++)
{
startCell.Offset(i + 1, 0).Value = data[i].Key;
startCell.Offset(i + 1, 1).Value = data[i].Value;
}
using (ExcelRange Rng = worksheets.Cells[1, 16, 5, data.Count + 13])
{
Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
}
}
//Add the chart to the sheet
var pieChart = worksheet.Drawings.AddChart("Chart1", eChartType.Pie);
pieChart.SetPosition(data.Count + 1, 0, 0, 0);
pieChart.Title.Text = "Test Case Excution Status";
pieChart.Title.Font.Bold = true;
pieChart.Title.Font.Size = 12;
//Set the data range
var series = pieChart.Series.Add(worksheets.Cells[2, 17, data.Count + 1, 17], worksheets.Cells[2, 16, data.Count + 1, 16]);
var pieSeries = (ExcelPieChartSerie)series;
pieSeries.Explosion = 5;
//Format the labels
pieSeries.DataLabel.Font.Bold = true;
pieSeries.DataLabel.ShowValue = true;
pieSeries.DataLabel.ShowPercent = true;
pieSeries.DataLabel.ShowLeaderLines = true;
pieSeries.DataLabel.Separator = ";";
pieSeries.DataLabel.Position = eLabelPosition.BestFit;
//Format the legend
pieChart.Legend.Add();
pieChart.Legend.Border.Width = 0;
pieChart.Legend.Font.Size = 12;
pieChart.Legend.Font.Bold = true;
pieChart.Legend.Position = eLegendPosition.Right;
// add a new worksheet to the empty workbook
using (var cells = worksheets.Cells[1, 1, 1, totalColumns.Length]) //(1,1) => (1,10)
{
cells.Style.Font.Bold = true;
}
using (var cells = worksheets.Cells[1, 7, 1, totalColumn.Length + 7]) //(1,1) => (1,10)
{
cells.Style.Font.Bold = true;
}
using (var cells = worksheets.Cells[1, 13, 1, totalColumn3.Length + 13]) //(1,1) => (1,10)
{
cells.Style.Font.Bold = true;
}
using (var cells = worksheets.Cells[1, 16, 1, totalColumn4.Length + 16]) //(1,1) => (1,10)
{
cells.Style.Font.Bold = true;
}
var records = data1.ToList();
var list = datas.ToList();
var funValue = funcitonData.ToList();
int totalRows = data1.Count + 1; //data including header row
int totalRowsList = datas.Count + 2;
var totalRowsForFuntion = funcitonData.Count + 1;
for (var i = 0; i < totalColumns.Length; i++)
{
worksheets.Cells[1, i + 1].Value = totalColumns[i]; //populate header row
}
for (var i = 0; i < totalColumn.Length; i++)
{
worksheets.Cells[1, i + 7].Value = totalColumn[i]; //populate header row
}
for (var i = 0; i < totalColumn3.Length; i++)
{
worksheets.Cells[1, i + 13].Value = totalColumn3[i]; //populate header row
}
//Add values
var j = 2; //to start data from second row after the header row.
var k = 2;
var l = 2;
if (data1 != null)
{
foreach (var item in records)
{
worksheets.Cells["A" + j].Value = cellNumb;
worksheets.Cells["B" + j].Value = item.TestCaseName;
worksheets.Cells["C" + j].Value = item.TestPlanName;
worksheets.Cells["D" + j].Value = item.Status;
j++;
cellNumb++;
}
using (ExcelRange Rng = worksheets.Cells[1, 1, totalRows, totalColumns.Length])
{
Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
}
}
if (datas != null)
{
foreach (var item in datas)
{
worksheets.Cells["G" + k].Value = item.TestPlanNameForCount;
worksheets.Cells["H" + k].Value = item.TotalPassedCount;
worksheets.Cells["I" + k].Value = item.TotalFailedCount;
worksheets.Cells["J" + k].Value = item.TotalPendingCount;
worksheets.Cells["K" + k].Value = item.TotalBlockCount;
k++;
}
worksheets.Cells["G" + k].Value = "Total";
worksheets.Cells["H" + k].Value = datas.Sum(x => x.TotalPassedCount);
worksheets.Cells["I" + k].Value = datas.Sum(x => x.TotalFailedCount);
worksheets.Cells["J" + k].Value = datas.Sum(x => x.TotalPendingCount);
worksheets.Cells["K" + k].Value = datas.Sum(x => x.TotalBlockCount);
using (ExcelRange Rng = worksheets.Cells[1, 7, totalRowsList, totalColumn.Length + 6])
{
Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
}
}
if (funcitonData != null)
{
foreach (var item in funValue)
{
worksheets.Cells["M" + l].Value = item.FunctionName;
worksheets.Cells["N" + l].Value = item.TotalCountTestCaseByTestRunId;
l++;
}
using (ExcelRange Rng = worksheets.Cells[1, 13, totalRowsForFuntion, totalColumn3.Length + 12])
{
Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;
Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
}
}
worksheets.Cells.AutoFitColumns();
result = package.GetAsByteArray();
return result;
}
}
catch (Exception ex)
{
throw new Exception($"Download failed : {ex.Message}");
}
}
}
var redarChart = worksheet.Drawings.AddRadarChart("RadarChart",
eRadarChartType.RadarMarkers);
redarChart.SetPosition(42, 0, 0, 0);
redarChart.SetSize(700, 300);
redarChart.Title.Text = "Function Map";
redarChart.Title.Font.Bold = true;
redarChart.Title.Font.Size = 12;
var serie = redarChart.Series.Add(worksheets.Cells[2, 14, funValue.Count + 1, 14], worksheets.Cells[2, 13, funValue.Count + 1, 13]);
serie.HeaderAddress = new ExcelAddress("'sheet'!N1");
redarChart.StyleManager.SetChartStyle(ePresetChartStyleMultiSeries.RadarChartStyle4);
redarChart.Fill.Color = System.Drawing.Color.Black;
redarChart.Legend.Position = eLegendPosition.TopRight;
//If you want to apply custom styling do that after setting the chart style so its not overwritten.
redarChart.Legend.Effect.SetPresetShadow(ePresetExcelShadowType.OuterTopLeft);
var radarSeries = (ExcelRadarChartSerie)serie;
radarSeries.Marker.Size = 5;

Chart Display after plot error

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);

How to parse JSON foreach object in array?

I'm currently looping through a JSON array and generating an object for each item in the array.
I wondering how I can parse data from the current object.
int i = 0, count = 0, yMP, yGP, yGT, favscountB8 = 0;
string gamertag;
JObject o1 = JObject.Parse(File.ReadAllText(#"gamerprofile.json"));
string jsonGamerprofile = JsonConvert.SerializeObject(o1);
var resultObjects = AllChildren(JObject.Parse(jsonGamerprofile))
.First(c => c.Type == JTokenType.Array && c.Path.Contains("favorites"))
.Children<JObject>();
foreach (JObject result in resultObjects)
{
// Here I wish to make the string gamertag = the value of (FavsGTS_gpF) which is inside the current object.
favscountB8 = favscountB8 + 1;
count = count + 1;
yMP = 6 + (62 * (count - 1));
yGP = 7 + (62 * (count - 1));
yGT = 11 + (62 * (count - 1));
Panel mainpanel1 = new Panel();
PictureBox gamerpic1 = new PictureBox();
Label gamertag1 = new Label();
mainpanel1.BackColor = Color.Red;
mainpanel1.Location = new Point(6, yMP);
mainpanel1.Size = new Size(957, 60);
mainpanel1.Name = "mainpanel" + i.ToString();
gamerpic1.BackColor = Color.Azure;
gamerpic1.Location = new Point(7, yGP);
gamerpic1.Size = new Size(54, 54);
gamerpic1.Name = "gamerpic" + i.ToString();
gamerpic1.BringToFront();
gamertag1.ForeColor = Color.FromArgb(110, 120, 127);
gamertag1.BackColor = Color.White;
gamertag1.Text = "Gamertag " + count.ToString();
gamertag1.Font = new Font("Arial Narrow", 14, FontStyle.Bold);
gamertag1.Location = new Point(76, yGT);
gamertag1.Size = new Size(145, 23);
gamertag1.Name = "Gamertag " + count.ToString();
gamertag1.BringToFront();
i = i + 1;
panel4.Controls.Add(gamertag1);
panel4.Controls.Add(gamerpic1);
panel4.Controls.Add(mainpanel1);
}
Inside the foreach statement I wish to make the "gamertag" string equal to the value of "FavsGTS_gpF" which is in each object in the array.

How to set the position of an Excel chart with cell value from C#?

I'm using following method to create and position the excel chart beside table data. below code is working fine, but chart position(height) is creating problem when number of charts increase.
There is any solution for draw chart with cell value? or other thing that may be used by me to keep chart beside table data.
I want to print result of following method page by page and therefore want to place it correctly.
below is my code:
Excel.Application xla = new Excel.Application();
int height=75;
string uppercell = "B12";
int cell = 12;
if (CheckBox1.Checked == true)
{
string table_name, chart_name;
table_name = "Ration Table";
chart_name = "Ratio";
string st = "";
con.Open();
trying(ws, st, height, uppercell, cell, table_name, chart_name);
con.Close();
cell = cell + 33;
uppercell = "B" + cell;
height = 496 + 75;//height + 300;
}
public void trying(Excel.Worksheet ws,string st,double height,string uppercell,int cell,string table_name,string chart_name)
{
MySqlDataAdapter da = new MySqlDataAdapter(st, con);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
//int cell = System.Int32.Parse(uppercell.Substring(1));
//********************** Now create the chart. *****************************
Excel.ChartObjects chartObjs = (Excel.ChartObjects)ws.ChartObjects(Type.Missing);
//Excel.ChartObject chartObj = chartObjs.Add(left, top, width, height);
Excel.ChartObject chartObj = chartObjs.Add(260, height, 350, 210);
Excel.Chart xlChart = chartObj.Chart;
//ws.Shapes.Item("xlChart").Top = (float)(double)ws.get_Range("E6").Top;//this.Controls.AddChart(this.Range["D2", "H12"]"chart1");
int nRows = dtMainSQLData.Rows.Count;
int nColumns = dtMainSQLData.Columns.Count;
string upperLeftCell = uppercell;// "B10";
int endRowNumber = System.Int32.Parse(upperLeftCell.Substring(1)) + nRows - 1;
char endColumnLetter = System.Convert.ToChar(Convert.ToInt32(upperLeftCell[0]) + nColumns - 1);
string upperRightCell = System.String.Format("{0}{1}", endColumnLetter, System.Int32.Parse(upperLeftCell.Substring(1)));
string lowerRightCell = System.String.Format("{0}{1}", endColumnLetter, endRowNumber);
Excel.Range rg = ws.get_Range(upperLeftCell, lowerRightCell);
for (int i = cell - 1; i < dtMainSQLData.Rows.Count + cell; i++)
{
for (int j = 2; j < dtMainSQLData.Columns.Count + 2; j++)
{
if (i == cell - 1)
{
xla.Cells[i, j] = dcCollection[j - 2].ToString();
}
else
{
xla.Cells[i, j] = dtMainSQLData.Rows[i - cell][j - 2].ToString();
}
}
}
//ws.Columns.AutoFit();
//for (int i = 1; i <= dtMainSQLData.Rows.Count; i++)
//{
// rg[1, i] = dtMainSQLData.Rows[i - 1][0].ToString(); //For Adding Header Text
// rg[2, i] = int.Parse(dtMainSQLData.Rows[i - 1][1].ToString()); //For Adding Datarow Value
//}
int lcellh = cell - 10;
int rcellh = cell - 10;
string lcellh1 = "B" + lcellh;
string rcellh1 = "L" + rcellh;
//-----------------------for company name and address-------------------------//
Excel.Range chartRange3;
ws.get_Range(lcellh1, rcellh1).Merge(false);//----------------------------------upperleft_cell, lowerright_cell
chartRange3 = ws.get_Range(lcellh1, rcellh1);
chartRange3.FormulaR1C1 = "BLUE BIRD FOODS (I) PVT.LTD.";//----------------------------------company name
chartRange3.HorizontalAlignment = 3;
chartRange3.VerticalAlignment = 3;
chartRange3.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
chartRange3.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.PaleVioletRed);
chartRange3.Font.Size = 20;
Excel.Range formatRange3;
formatRange3 = ws.get_Range(lcellh1, rcellh1);//----------------------------------hrow_border1, hrow_border2
formatRange3.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
//------for making header font bold
int hcell = cell - 1;
string hleftrange = "B" + hcell;
string hrightrange = "D" + hcell;
Excel.Range formatRange1;
formatRange1 = ws.get_Range(hleftrange);//----------------------------------hrow_bold
formatRange1.EntireRow.Font.Bold = true;
formatRange1.HorizontalAlignment = 3;
formatRange1.VerticalAlignment = 3;
//ws.Cells[1, 3] = "Bold";
//-------for giving broder to header
Excel.Range formatRange2;
formatRange2 = ws.get_Range(hleftrange, hrightrange);//----------------------------------hrow_border1, hrow_border2
formatRange2.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
//-------for giving broder to table data
Excel.Range formatRange;
formatRange = ws.get_Range(upperLeftCell, lowerRightCell);//----------------------------------upperLeftCell, lowerRightCell
formatRange.BorderAround(Excel.XlLineStyle.xlContinuous,
Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic,
Excel.XlColorIndex.xlColorIndexAutomatic);
formatRange.HorizontalAlignment = 3;
formatRange.VerticalAlignment = 3;
//--------for giving name to the table
int lcell = cell - 3;
int rcell = cell - 2;
string tleftrange = "B" + lcell;
string trightrange = "D" + rcell;
Excel.Range chartRange1;
ws.get_Range(tleftrange, trightrange).Merge(false);//----------------------------------upperleft_cell, lowerright_cell
formatRange2 = ws.get_Range(tleftrange, trightrange);
formatRange2.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
chartRange1 = ws.get_Range(tleftrange, trightrange);
chartRange1.FormulaR1C1 = table_name;//"Ratio table";//----------------------------------table_name
chartRange1.HorizontalAlignment = 3;
chartRange1.VerticalAlignment = 3;
chartRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
chartRange1.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.PaleVioletRed);
chartRange1.Font.Size = 20;
//--------drawing chart with range
Excel.Range chartRange = ws.get_Range(upperLeftCell, lowerRightCell);
xlChart.SetSourceData(chartRange, System.Reflection.Missing.Value);
xlChart.ChartType = Excel.XlChartType.xl3DPie;
// *******************Customize axes: ***********************
Excel.Axis xAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlCategory,
Excel.XlAxisGroup.xlPrimary);
//xAxis.HasTitle = true;
// xAxis.AxisTitle.Text = "X Axis";
Excel.Axis yAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlSeriesAxis,
Excel.XlAxisGroup.xlPrimary);
//yAxis.HasTitle = true;
//yAxis.AxisTitle.Text = "Y Axis";
Excel.Axis zAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlValue,
Excel.XlAxisGroup.xlPrimary);
//zAxis.HasTitle = true;
//zAxis.AxisTitle.Text = "Z Axis";
// *********************Add title: *******************************
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = chart_name;// "Ratio";
// *****************Set legend:***************************
xlChart.HasLegend = true;
}

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

Categories

Resources