- Home
- Forum
- ASP.NET MVC
- Presentation - build chart with different colored bars
Presentation - build chart with different colored bars
Hi
I have seen other threads relating to this but I've had no success in doing this myself.
I have a dictionary which adds male: 40 and female: 60 (example figures) to a bar chart in a presentation:
chart.ChartData.SetValue(rowCount, columnCount, $"{dataText} - {data.Count}");
columnCount++;
chart.ChartData.SetValue(rowCount, columnCount, Math.Round(data.Percentage));
Using the series I can set every bar color to the same value but I cannot set one bar color differently.
chart.Series[0].DataPoints.DefaultDataPoint.DataFormat.Fill.ForeColor = PowerpointUtility.Red;
If I increase the Series index I get an out of bounds exception. There is nothing in the docs for this either.
Is there a way I can set a bar color based on the value. E.G
if (data.Percentage > 50)
chart.Series[0].DataPoints.DefaultDataPoint.DataFormat.Fill.ForeColor = Red;
else
chart.Series[0].DataPoints.DefaultDataPoint.DataFormat.Fill.ForeColor = Green;
Thanks for your help.
A struggling developer :-)
SIGN IN To post a reply.
4 Replies
RM
Ramaraj Marimuthu
Syncfusion Team
March 15, 2018 12:56 PM UTC
Hi Kurtis,
Thank you for your update.
Thank you for your update.
We have created a sample for your requirement. Please find the code snippet and sample from below,
|
private void CreatePresentation()
{
//Create a PowerPoint presentation instance
IPresentation presentation = Presentation.Create();
//Create a blank slide
ISlide slide = presentation.Slides.Add(SlideLayoutType.Blank);
//Add a chart in specific location
IPresentationChart chart = slide.Charts.AddChart(270, 70, 500, 400);
//Set the chart data
chart.ChartData.SetValue(1, 1, "Gender");
chart.ChartData.SetValue(1, 2, "Male");
chart.ChartData.SetValue(1, 3, "Female");
chart.ChartData.SetValue(2, 1, "Accommodation");
chart.ChartData.SetValue(3, 1, "Food");
chart.ChartData.SetValue(4, 1, "Cosmetics");
chart.ChartData.SetValue(5, 1, "Cloths");
chart.ChartData.SetValue(6, 1, "Medicines");
chart.ChartData.SetValue(2, 2, 30);
chart.ChartData.SetValue(3, 2, 60);
chart.ChartData.SetValue(4, 2, 45);
chart.ChartData.SetValue(5, 2, 55);
chart.ChartData.SetValue(6, 2, 15);
chart.ChartData.SetValue(2, 3, 60);
chart.ChartData.SetValue(3, 3, 30);
chart.ChartData.SetValue(4, 3, 75);
chart.ChartData.SetValue(5, 3, 15);
chart.ChartData.SetValue(6, 3, 35);
//Specifies the chart title
chart.ChartTitle = "Expenses";
//Create a new chart-series with the name
IOfficeChartSerie serieForMale = chart.Series.Add("Male");
//Set the data range for chart
serieForMale.Values = chart.ChartData[2, 2, 6, 2];
//Create a new chart-series with the name
IOfficeChartSerie serieForFemale = chart.Series.Add("Female");
//Set the data range for chart
serieForFemale.Values = chart.ChartData[2, 3, 6, 3];
//Set the data range for category axis
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 6, 1];
//Set the chart type as bar chart
chart.ChartType = OfficeChartType.Bar_Clustered;
//Set color for the serie of male
SetColorForSerie(serieForMale);
//Set color for the serie of femle
SetColorForSerie(serieForFemale);
//Save the presentation into memory stream
presentation.Save("sample.pptx");
//Close the presentation intstance
presentation.Close();
}
/// <summary>
/// Set the color to serie datapoints
/// </summary>
/// <param name="serie">Represent the serie of the chart</param>
private void SetColorForSerie(IOfficeChartSerie serie)
{
for (int i = serie.Values.FirstRow; i <= serie.Values.LastRow; i++)
{
int percentage = Convert.ToInt32(serie.Values.GetValue(i, serie.Values.FirstColumn));
if (percentage > 50)
serie.DataPoints[i - serie.Values.FirstRow].DataFormat.Fill.ForeColorIndex = OfficeKnownColors.Orange;
else
serie.DataPoints[i - serie.Values.FirstRow].DataFormat.Fill.ForeColorIndex = OfficeKnownColors.Violet;
}
} |
Presentation document – http://www.syncfusion.com/downloads/support/forum/136460/ze/OutputDocument-889119578
Please let us know if you need any further assistances in this.
Regards,
Ramaraj Marimuthu.
Please let us know if you need any further assistances in this.
Regards,
Ramaraj Marimuthu.
UN
Unknown
Syncfusion Team
March 15, 2018 06:06 PM UTC
Hi Ramaraj,
Attachment: picture_cd04f8ff.zip
Thank you so much for your code example. This has changed the color now which is great. I wasn't using series to start with.
My problem is now its only ever displaying the first serie.
I'm generating dynamic chart so I can't used hardcore values for the serie or the chart data. I also only need a single level bar chart.
E.G I have a dictionary of places UK: 30, USA: 80, CANADA: 40 and putting that into the chart I use:
var rowCount = 0;
var columnCount = 0;
foreach (var data in answerDtos.Values)
{
rowCount++;
columnCount = 1;
chart.ChartData.SetValue(rowCount, columnCount, data.Text);
columnCount++;
chart.ChartData.SetValue(rowCount, columnCount, data.Percentage);
var serie = chart.Series.Add(data.Text);
serie.Values = chart.ChartData[rowCount, columnCount, rowCount, columnCount];
SetColorForSerie(serie);
}
// Select the text only for the labels.
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[1, 1, rowCount, 1];
This gives me this data sheet:
C1 C2
R1 UK 30
R2 USA 80
R3 CANADA 40
But only 1 label which is always the first item (UK in this example). I have added a image to show you what I mean. I'm clearly doing something wrong :-(
Finally, is there a way to put gaps between the bars? :-)
Thank you so much for your help
Kurtis
Attachment: picture_cd04f8ff.zip
UN
Unknown
Syncfusion Team
March 15, 2018 08:27 PM UTC
I managed to sort this. :-D I was treating each bar as a separate series as a collection of bars, if that makes sense.
Thanks for your help!
Kurtis
RM
Ramaraj Marimuthu
Syncfusion Team
March 16, 2018 12:30 PM UTC
Hi Kurtis,
Thank you for your update.
Thank you for your update.
We are happy to know that you have achieved your requirement with our Presentation library.
Please let us know if you need any further assistances in this.
Regards,
Ramaraj Marimuthu
Regards,
Ramaraj Marimuthu
SIGN IN To post a reply.
- 4 Replies
- 2 Participants
-
UN Unknown
- Mar 14, 2018 08:25 PM UTC
- Mar 16, 2018 12:30 PM UTC