Need help in generating pie chart

Hi

I am using Aspose.chart to generate pie chart. I have 6 values to plot in pie chart.
1. Very Satisfied
2. Moderately Satisfied
3. Slightly Satisfied
4. Very Dissatisfied
5. Moderately Dissatisfied
6. Slightly Dissatisfied

In some cases, only one value will be there. ie. For Very satisfied, the count will be 10 and others will be zero. In that case, graph is not generated. We have to plot the graph with Very Satisfied as 100%. Please help me in solving this issue.

Hi,

Have you used the following lines in your code?

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

//Set Default DataPoint Label Format

pieSeries.DefaultDataPoint.LabelFormat = "P0";

//Set Default DataPoint Label Value

pieSeries.DefaultDataPoint.LabelValue = DataLabelValueType.YPercent;



Here is the full code to generate your required chart.

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

public Chart GetChart()

{

//start code

// Instantiating the Chart Object

Chart pieChart = new Chart();

//Add new Chart Title

pieChart.Titles.Add(new Title());

//Add Chart Title Text

pieChart.Titles[0].Text = "Pie 2D";

//Set Chart Area Transparence

pieChart.ChartArea.Transparence = 255;

//Create New Series

Series pieSeries = new Series();

//Set Chart Type as Pie Chart

pieSeries.ChartType = ChartType.Pie;

//Set Pie Chart Custom Properties

pieSeries.CustomAttributes.PieLabelType = PieLabelType.Circle;

pieSeries.CustomAttributes.RadialLineSize = 0;

pieSeries.CustomAttributes.HorizontalLineSize = 0;

pieSeries.DefaultDataPoint.CustomAttributes.IsWithLabelMark = false;

pieSeries.CustomAttributes.PieSurroundedSpaceIsEmpty = true;

//SetDefault DataPoint Label Visibility

pieSeries.DefaultDataPoint.IsLabelVisible = true;

//Set Default DataPoint Label Format

pieSeries.DefaultDataPoint.LabelFormat = "P0";

//Set Default DataPoint Label Value

pieSeries.DefaultDataPoint.LabelValue = DataLabelValueType.YPercent;

pieSeries.DefaultDataPoint.Font = new Font("Arial", 12);

//Add DataPoints to Series DataPoint Collection

pieSeries.DataPoints.Add(new DataPoint("Very Satisfied", 10));

pieSeries.DataPoints.Add(new DataPoint("Moderately Satisfied", 0));

pieSeries.DataPoints.Add(new DataPoint("Slightly Satisfied", 0));

pieSeries.DataPoints.Add(new DataPoint("Very Dissatisfied", 0));

pieSeries.DataPoints.Add(new DataPoint("Moderately Dissatisfied", 0));

pieSeries.DataPoints.Add(new DataPoint("Slightly Dissatisfied", 0));

//Add Series to Chart’s Series Collection

pieChart.SeriesCollection.Add(pieSeries);

//end code

//Return Chart Object

return pieChart;

}



Please feel free to contact us in case you have further comments or questions.

Best Regards,

Hi,

I have tried the code snippet that you suggested. It doesn't worked out. This is the code which i am using in my project.


public Chart GeneratePieChart(DataTable dtNewReport, int qid)
{

decimal total = -1;
Aspose.Report.License license = new Aspose.Report.License();
license.SetLicense(HttpContext.Current.Server.MapPath("~\\License\\Aspose.Total.lic"));

Chart c = new Chart();
c.Titles.Add(new Title());
//Set Chart's Title Text
string title = "Services Rating graph";
if (qid == -1)
{
c.Titles[0].Text = title;
}
else
{
c.Titles[0].Text = title + " for " + Common.General.GetTravelAgencySubQuestion(qid);
}
c.Titles[0].Font = new System.Drawing.Font("Arial", 9, FontStyle.Bold);

c.ChartArea.IsThreeD = true;
c.ChartAreaLayout = LayoutType.Table;
c.ChartArea.AxisX.AxisLabels.IsDataPointNameVisible = true;

//Reduce the transparence and see the required colors
c.ChartArea.Transparence = 255;

c.ChartArea.Margin = 0;
c.ChartArea.MarginTop = 0;
c.ChartArea.MarginBottom = 0;
c.ChartArea.MarginLeft = 0;
c.ChartArea.MarginRight = 0;
c.ChartArea.PaletteType = PaletteType.HighContrast;
c.ChartArea.LegendBox.IsVisible = true;
c.ChartArea.Titles.Capacity = 0;
c.ChartArea.IsFitLabels = false;

////Specifying LegendBox Properties
LegendBox legendBox = c.ChartArea.LegendBox;

legendBox.Font = new Font("Arial", 8);
legendBox.FontColor = Color.Blue;
legendBox.GradientBackColor = Color.Chocolate;
legendBox.GradientType = GradientType.HorizontalCenter;
legendBox.IsVisible = true;
legendBox.LayoutType = LayoutType.Table;
legendBox.LegendPositionType = LegendPositionType.Bottom;
legendBox.LegendBoxType = LegendBoxType.OutsideChartArea;
legendBox.MarkerSize = new SizeF(10, 10);
legendBox.Position = new RectangleF(0, Convert.ToSingle(c.Height * .35), c.Width, 60);
//legendBox.Position = new re
legendBox.RowCount = 2;
legendBox.ColumnCount = 3;

total = Convert.ToInt32(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfVerySat"]) +
Convert.ToInt32(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfModSat"]) +
Convert.ToInt32(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfSliSat"]) +
Convert.ToInt32(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfSliDiss"]) +
Convert.ToInt32(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfModDiss"]) +
Convert.ToInt32(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfVeryDiss"]);

Aspose.Chart.Series s = new Series();
s.ChartType = ChartType.Pie;
s.DefaultDataPoint.CustomAttributes.IsWithLabelMark = false;
//SetDefault DataPoint Label Visibility
s.DefaultDataPoint.IsLabelVisible = true;
//Set Default DataPoint Label Format
s.DefaultDataPoint.LabelFormat = "P0";
//Set Default DataPoint Label Value
s.DefaultDataPoint.LabelValue = DataLabelValueType.YPercent;
s.DefaultDataPoint.Font = new Font("Arial", 12);

DataPoint dtVS = new DataPoint(0, dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfVerySat"]);
dtVS.Color = Color.FromArgb(0, 130, 0);
dtVS.Name = "Very Satisfied - " + Math.Round(Convert.ToDecimal(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfVerySat"]) * 100 / total, 2).ToString() + "%";
s.DataPoints.Add(dtVS);

DataPoint dtMS = new DataPoint(1, dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfModSat"]);
dtMS.Color = Color.FromArgb(49, 203, 49);
dtMS.Name = "Moderately Satisfied - " + Math.Round(Convert.ToDecimal(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfModSat"]) * 100 / total, 2).ToString() + "%";
s.DataPoints.Add(dtMS);

DataPoint dtSS = new DataPoint(2, dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfSliSat"]);
dtSS.Color = Color.FromArgb(99, 255, 49);
dtSS.Name = "Slightly Satisfied - " + Math.Round(Convert.ToDecimal(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfSliSat"]) * 100 / total, 2).ToString() + "%";
s.DataPoints.Add(dtSS);

DataPoint dtSD = new DataPoint(3, dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfSliDiss"]);
dtSD.Color = Color.FromArgb(255, 255, 107);
dtSD.Name = "Slightly Dissatisfied - " + Math.Round(Convert.ToDecimal(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfSliDiss"]) * 100 / total, 2).ToString() + "%";
s.DataPoints.Add(dtSD);

DataPoint dtMD = new DataPoint(4, dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfModDiss"]);
dtMD.Color = Color.FromArgb(255, 255, 0);
dtMD.Name = "Moderately Dissatisfied - " + Math.Round(Convert.ToDecimal(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfModDiss"]) * 100 / total, 2).ToString() + "%";
s.DataPoints.Add(dtMD);

DataPoint dtVD = new DataPoint(5, dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfVeryDiss"]);
dtVD.Color = Color.FromArgb(206, 0, 0);
dtVD.Name = "Very Dissatisfied - " + Math.Round(Convert.ToDecimal(dtNewReport.Rows[dtNewReport.Rows.Count - 1]["numOfVeryDiss"]) * 100 / total, 2).ToString() + "%";
s.DataPoints.Add(dtVD);

//To rotate the pie in Pie chart
s.CustomAttributes.PieFirstSliceAngle = -90;

c.SeriesCollection.Add(s);
c.ChartArea.LegendBox.LegendItems.Clear();
for (int counter = 0; counter < s.DataPoints.Count; counter++)
{
c.ChartArea.LegendBox.LegendItems.Add(s.DataPoints[counter].Name, s, counter);

}

return c;
}

Hi,

Please some one help me in solving the issue. I have used the code suggested. But still it's not working. I have specified my code used in the project in the above blog.

Hi,

I got your code working by commenting out the following line.

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

// s.CustomAttributes.PieFirstSliceAngle = -90;



This line works if more than one data points have non-zero Y values.

Please feel free to contact us in case you have further comments or questions.

Best Regards,