problem with save as image chart in spire.xls - c#

I Used spire.xls to create a sheet and a chart but my problem is when I save a chart as a PDF it would be so inappropriate just need to justify the chart but don't know how I think it would be the problem with scale
Chart chart = sheet.Charts.Add(ExcelChartType.ScatterSmoothedLineMarkers);
//Set region of chart data
chart.DataRange = sheet.Range["A2:A" + (_state.NumberOfReads + 2).ToString()].AddCombinedRange(sheet.Range["F2:F" + (_state.NumberOfReads + 2).ToString()]);
chart.SeriesDataFromRange = false;
//Set position of the chart
chart.LeftColumn = 22;
chart.TopRow = 6;
chart.RightColumn = 32;
chart.BottomRow = 29;
//Set and format category axis title
chart.PrimaryCategoryAxis.Title = "Applied Pressure DUT.";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
//Set and format value axis title
chart.PrimaryValueAxis.Title = "Average of REF.";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = -90;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.Legend.Delete();
//Loop through the data series of the chart
foreach (ChartSerie cs in chart.Series)
{
cs.Format.Options.IsVaryColor = true;
//Show data labels for data points
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
}
chart.PrimaryValueAxis.HasMajorGridLines = true;
IChartTrendLine trendLine = chart.Series[0].TrendLines.Add(TrendLineType.Linear);
chart.Series[0].TrendLines[0].DisplayEquation = true;
chart.Series[0].TrendLines[0].DisplayRSquared = true;
//chart.HasDataTable = true;
Image[] imgs = workbook.SaveChartAsImage(sheet);
Bitmap img = new Bitmap(imgs[0]);
img.Save(path + filePic, ImageFormat.Png);
and also look at the outputs

Related

How to add labels both inside and outside chart

I want to add labels both inside and outside of a pie chart.
I have tried adding values to it, it works either inside or outside.
I want to display both VALX and VALY on my chart
How can I do this?
I want my chart to appear like this.
XmlNodeList xnList = xml.SelectNodes("/Report/Parameters/Parameter");
var chart = new Chart();
chart.Height = 600;
chart.Width = 900;
ArrayList xAxisData = new ArrayList();
ArrayList yAxisData = new ArrayList();
string title = "Motility";
chart.Titles.Add(title);
var chartArea1 = new ChartArea();
chart.ChartAreas.Add(chartArea1);
chart.ChartAreas[0].AlignmentStyle = AreaAlignmentStyles.All;
Series series1;
xAxisData.Clear();
yAxisData.Clear();
string seriesName1 = " ";
byte ColorIndex = 0;
series1 = new Series();
seriesName1 = "Pie Chart";
series1.Name = seriesName1;
series1.ChartType = SeriesChartType.Pie;
chart.Series.Add(series1);
chart.ChartAreas[0].Area3DStyle.Enable3D = true;
chart.ChartAreas[0].Area3DStyle.Inclination = 40;
chart.ChartAreas[0].BorderColor = Color.Black;
chart.ChartAreas[0].Area3DStyle.WallWidth = 20;
chart.Series[seriesName1]["3DLabelLineSize"] = "30";
chart.Series[seriesName1].IsVisibleInLegend = false;
foreach (KeyValuePair<string, ReportParameter> pair in T_NewChart.parameters)
{
chart.Series[seriesName1].Points.AddXY((pair.Value.Name), pair.Value.Values[0]));
if (ColorIndex == 0)
{
chart.Series[seriesName1].Points[0].Color = Color.Green;
chart.Series[seriesName1].Label = "#VALX";
chart.Series[seriesName1]["PieLabelStyle"] = "Outside";
chart.Series[seriesName1].BorderColor = Color.Black;
chart.Series[seriesName1].Points[0].Label = "Total";
chart.Series[0].Font = new System.Drawing.Font("Arial", 15F);
}
else if (ColorIndex == 1)
{
chart.Series[seriesName1].Points[1].Color = Color.Yellow;
chart.Series[seriesName1].Points[1].Label = "Non";
}
else if (ColorIndex == 2)
{
chart.Series[seriesName1].Points[2].Color = Color.Red;
chart.Series[seriesName1].Points[2].Label = "Few";
}
ColorIndex++;
}
chart.SaveImage("C:\\NewPie_chart.png", ChartImageFormat.Png);
I can only display the "Name", but I want to display the "Name" (VALX) outside the chart and the "Value" (VALY) inside the chart.

Zoom in multiple Areas and Axis Y

I have a project with multiple AxisY, with 3 series and and each series has its axisY associated. When I zoom, I have achieved that all the series have zoom but the scale of the axisYof the secondary axisY doesn't change, only that of the main series changes.
Original Chart:
Chart with zoom applied:
I have create the new axisY secondary with the code of microsoft (Project "Windows Forms Samples Environment for Microsoft Chart Control"):
public 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;
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());
// 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;
}
UPDATE--------------------------------:
I have this news:
Now, the 3 axis update his scale but it comes out together, How to separate them?
The code is here:
chart1.ChartAreas[2].AlignmentOrientation = AreaAlignmentOrientations.All;
chart1.ChartAreas[2].AlignmentStyle = AreaAlignmentStyles.All;
chart1.ChartAreas[2].AlignWithChartArea = "ChartArea1";
chart1.ChartAreas[4].AlignmentOrientation = AreaAlignmentOrientations.All;
chart1.ChartAreas[4].AlignmentStyle = AreaAlignmentStyles.All;
chart1.ChartAreas[4].AlignWithChartArea = "ChartArea1";

Microsoft chart with chart type is Column

I tried to decrease space between space between columns and also wanted to decrease width of columns, but I was only able to adjust width of columns not space between columns.
please help for solution. I really appreciate the effort.
Code snap: `
for (int i = 0; i < dss.Tables[1].Rows.Count; i++)
{
yValues[i] = Convert.ToDouble(dss.Tables[1].Rows[i]["admission"].ToString());
if (isDateRange == 1)
{
xValues[i] = dss.Tables[1].Rows[i]["mth"].ToString().Substring(0, dss.Tables[1].Rows[i]["mth"].ToString().IndexOf(" "));
}
else
{
xValues[i] = dss.Tables[1].Rows[i]["mth"].ToString();
}
zValues[i] = Convert.ToDouble(dss.Tables[1].Rows[i]["readmission"].ToString());
}
chart1.Series[0].Points.DataBindXY(xValues, yValues);
chart1.Series[0].ToolTip = "#AXISLABEL: #VALY";
chart1.Series[0]["PieLabelStyle"] = "Outside";
chart1.Series[0]["PieDrawingStyle"] = "Concave";
chart1.Series[1].Points.DataBindXY(xValues, zValues);
chart1.Series[1].ToolTip = "#AXISLABEL: #VALY";
var chartArea_chart1 = chart1.ChartAreas[chart1.Series[0].ChartArea];
// enable autoscroll
chartArea_chart1.CursorY.AutoScroll = true;
// let's zoom to [0,blockSize] (e.g. [0,100])
chartArea_chart1.AxisX.ScaleView.Zoomable = true;
chartArea_chart1.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
//chartArea_chart1.AxisY.ScaleView.Zoomable = true;
//chartArea_chart1.AxisY.ScaleView.SizeType = DateTimeIntervalType.Auto;
//chart1.Series[0].CustomProperties ="PixelPointWidth = 10";
//chart1.Series[0].CustomProperties = "PointWidth = 0.1";
int position = 0;
int size = 30;
chartArea_chart1.AxisX.ScaleView.Zoom(position, size);
// disable zoom-reset button (only scrollbar's arrows are available)
chartArea_chart1.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;`

itextsharp table chart legends beneath bars

Can someone explain me how I can create chart like this one using itextsharp library.
As you can see here, this chart has attached table with legends to its bottom side. And each bar has year attached to it. I want to create something like that.
Second picture. This is what i have so far. I don't have year attached to each bar, and my legends are not beneath each bar like in top graph. If someone could guide me how to create identical graph like one on top i would be grateful. Thanks in advance!
How can I turn those labels to be displayed horizontally, and put those legends beneath years in table like one in picture number 1.
// Chart Centers By Year
var chartCentersByYear = new Chart
{
Width = 1000,
Height = 450,
RenderType = RenderType.ImageTag,
AntiAliasing = AntiAliasingStyles.Graphics,
TextAntiAliasingQuality = TextAntiAliasingQuality.High
};
chartCentersByYear.Titles.Add("Centers By Year");
chartCentersByYear.Titles[0].Font = new Font("Arial", 16f);
chartCentersByYear.Titles[0].Alignment = System.Drawing.ContentAlignment.TopLeft;
chartCentersByYear.ChartAreas.Add("");
chartCentersByYear.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chartCentersByYear.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
chartCentersByYear.Series.Add("Count");
chartCentersByYear.Series.Add("Cases");
chartCentersByYear.Series[0].ChartType = SeriesChartType.Column; //Pie
chartCentersByYear.Series[1].ChartType = SeriesChartType.StepLine; //StepLine
chartCentersByYear.Series[1].BorderDashStyle = ChartDashStyle.DashDot;
chartCentersByYear.Series[1].BorderWidth = 3;
chartCentersByYear.Series[0].Color = Color.Brown;
chartCentersByYear.Legends.Add("1");
chartCentersByYear.Legends.Add("2");
chartCentersByYear.Legends[0].HeaderSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[0].HeaderSeparatorColor = Color.Black;
chartCentersByYear.Legends[0].ItemColumnSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[0].ItemColumnSeparatorColor = Color.Black;
chartCentersByYear.Legends[1].HeaderSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[1].HeaderSeparatorColor = Color.Black;
chartCentersByYear.Legends[1].ItemColumnSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[1].ItemColumnSeparatorColor = Color.Black;
//For the Legend
LegendCellColumn firstColumn = new LegendCellColumn();
firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
firstColumn.HeaderBackColor = Color.WhiteSmoke;
chartCentersByYear.Legends[0].CellColumns.Add(firstColumn);
chartCentersByYear.Legends[1].CellColumns.Add(firstColumn);
LegendCellColumn secondColumn = new LegendCellColumn();
secondColumn.ColumnType = LegendCellColumnType.Text;
secondColumn.Text = "#LEGENDTEXT";
secondColumn.HeaderBackColor = Color.WhiteSmoke;
LegendItem newItemCount = new LegendItem();
newItemCount.Cells.Add(LegendCellType.Text, "Count", System.Drawing.ContentAlignment.MiddleCenter);
newItemCount.BorderWidth = 1;
newItemCount.BorderDashStyle = ChartDashStyle.Solid;
LegendItem newItemCases = new LegendItem();
newItemCases.Cells.Add(LegendCellType.Text, "Cases", System.Drawing.ContentAlignment.MiddleCenter);
newItemCases.BorderWidth = 1;
newItemCases.BorderDashStyle = ChartDashStyle.Solid;
// Getting data from a stored procedure
var totalCentersByYearResult = new Repository().GetTotalCentersByYear();
foreach (IGD_spInternationalReportCenterWithTots1_Result item in totalCentersByYearResult)
{
// For Series
chartCentersByYear.Series[0].Points.AddXY(item.YearEcmo, item.Count);
chartCentersByYear.Series[1].Points.AddY(item.Cases);
// For Legend
newItemCount.Cells.Add(LegendCellType.Text, item.Count.ToString(), System.Drawing.ContentAlignment.MiddleCenter);
newItemCases.Cells.Add(LegendCellType.Text, item.Cases.ToString(), System.Drawing.ContentAlignment.MiddleCenter);
}
chartCentersByYear.Legends[0].CustomItems.Add(newItemCount);
chartCentersByYear.Legends[0].CustomItems.Add(newItemCases);
chartCentersByYear.Legends[0].Docking = Docking.Bottom;
chartCentersByYear.Legends[1].Docking = Docking.Bottom; //Top
chartCentersByYear.Series[0].YAxisType = AxisType.Primary;
chartCentersByYear.Series[1].YAxisType = AxisType.Secondary;
//For two coordinate systems
chartCentersByYear.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
chartCentersByYear.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
chartCentersByYear.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
chartCentersByYear.ChartAreas[0].AxisY2.IsStartedFromZero = chartCentersByYear.ChartAreas[0].AxisY.IsStartedFromZero;
using (var chartimage = new MemoryStream())
{
chartCentersByYear.SaveImage(chartimage, ChartImageFormat.Png);
Byte[] newChart = chartimage.GetBuffer(); //return chartimage.GetBuffer();
var image = Image.GetInstance(newChart); //Image.GetInstance(Chart());
image.ScalePercent(50f);
image.SetAbsolutePosition(document.LeftMargin + 40, document.BottomMargin + 100);
document.Add(image);
}

winform mschart Column chart not centering column on datapoint

Hello I am having a hard time understanding how to get my column chart to be centered over the X-Axis label. IN the chart pic below you will see that I have columns based on 1/2 increments. For my positive series the data starts on the left and ends at the x-axis label. On my negative series the data starts at the x-axis label and continues to the right. How do I get these columns centered over the x-axis label. I don't understand why one series is displayed one way and the other series is displayed a different way.
Screen shot here:
http://i.imgur.com/QuAXUxr.png
Code is below
chartAreaDistributionHalfHour.Name = "ChartAreaDistributionHalfHour";
chartAreaDistributionHalfHour.AxisX.MajorGrid.LineColor = System.Drawing.Color.LightGray;
chartAreaDistributionHalfHour.AxisY.MajorGrid.LineColor = System.Drawing.Color.LightGray;
chartAreaDistributionHalfHour.AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
chartAreaDistributionHalfHour.AxisY.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
chartAreaDistributionHalfHour.AxisX.LabelAutoFitMaxFontSize = 7;
chartAreaDistributionHalfHour.AxisY.LabelAutoFitMaxFontSize = 7;
chartAreaDistributionHalfHour.AxisX.Title = "Distribution (Half-Hourly)";
chartAreaDistributionHalfHour.AxisY.Title = "Profit/Loss ($)";
chartAreaDistributionHalfHour.AxisY.IsMarginVisible = false;
seriesDistributionHalfHour.ChartArea = "ChartAreaDistributionHalfHour";
seriesDistributionHalfHour.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
seriesDistributionHalfHour.Color = System.Drawing.Color.Green;
seriesDistributionHalfHour.Enabled = true;
seriesDistributionHalfHour.Name = "distributionHalfHour";
seriesDistributionHalfHourNegative.ChartArea = "ChartAreaDistributionHalfHour";
seriesDistributionHalfHourNegative.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
seriesDistributionHalfHourNegative.Color = System.Drawing.Color.Red;
seriesDistributionHalfHourNegative.Enabled = true;
seriesDistributionHalfHourNegative.Name = "netLossDistributionHalfHour";
seriesDistributionHalfHourZeroLine.ChartArea = "ChartAreaDistributionHalfHour";
seriesDistributionHalfHourZeroLine.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
seriesDistributionHalfHourZeroLine.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
seriesDistributionHalfHourZeroLine.Color = System.Drawing.Color.Black;
seriesDistributionHalfHourZeroLine.Enabled = true;
seriesDistributionHalfHourZeroLine.Name = "netDistributionHalfHourZeroLine";
this.chartDistributionHalfHourGraph.Location = new System.Drawing.Point(17, 49);
this.chartDistributionHalfHourGraph.Name = "chartDistributionHalfHourGraph";
this.chartDistributionHalfHourGraph.BorderlineColor = System.Drawing.Color.Black;
this.chartDistributionHalfHourGraph.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
this.chartDistributionHalfHourGraph.Cursor = System.Windows.Forms.Cursors.Cross;
this.chartDistributionHalfHourGraph.Visible = false;
this.chartDistributionHalfHourGraph.Size = new System.Drawing.Size(1443, 694);
this.chartDistributionHalfHourGraph.TabIndex = 7;
this.chartDistributionHalfHourGraph.Text = "chart2";
this.chartDistributionHalfHourGraph.ChartAreas.Add(chartAreaDistributionHalfHour);
this.chartDistributionHalfHourGraph.Series.Add(seriesDistributionHalfHour);
this.chartDistributionHalfHourGraph.Series.Add(seriesDistributionHalfHourNegative);
this.chartDistributionHalfHourGraph.Series.Add(seriesDistributionHalfHourZeroLine);

Categories