Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Mon Feb 22, 2016 10:58 pm

I have a function that creates a slide with a bar chart where the values can be negative:

Code: Select all
private static void CreateUtilityChart (String[] columnlabels, String[] rowlabels, Double[,] values)
{
   Presentation presentation = new Presentation();
   presentation.SlideSize.Type = SlideSizeType.Screen16x9;
   SizeF slidesize = presentation.SlideSize.Size;

   var slide = presentation.Slides[0];


   RectangleF rect = new RectangleF(20, 20, slidesize.Width - 40, slidesize.Height - 40);
   IChart chart = slide.Shapes.AppendChart(Spire.Presentation.Charts.ChartType.BarClustered, rect);

   // Insert the column labels
   String[] cols = columnlabels.ToArray();
   for (Int32 c = 0; c < cols.Count(); ++c)
      chart.ChartData[0, c + 1].Text = cols[c];

   // Insert the row labels
   String[] rows = rowlabels.ToArray();
   for (Int32 r = 0; r < rows.Count(); ++r)
      chart.ChartData[r + 1, 0].Text = rows[r];

   // Insert the values
   Double min = 0.0;
   for (Int32 r = 0; r < rows.Count(); ++r)
   {
      for (Int32 c = 0; c < cols.Count(); ++c)
      {
         chart.ChartData[r + 1, c + 1].Value = values[r, c];
         min = Math.Min(min, values[r, c]);
      }
   }

   chart.Series.SeriesLabel = chart.ChartData[0, 1, 0, columnlabels.Count()];
   chart.Categories.CategoryLabels = chart.ChartData[1, 0, rowlabels.Count(), 0];

   chart.PrimaryCategoryAxis.Position = AxisPositionType.Left;
   chart.SecondaryCategoryAxis.Position = AxisPositionType.Left;

   // Do a series for each column
   for (Int32 c = 0; c < cols.Count(); ++c)
   {
      chart.Series[c].Values = chart.ChartData[1, c + 1, rowlabels.Count(), c + 1];
      chart.Series[c].Fill.FillType = FillFormatType.Solid;
      chart.Series[c].Fill.SolidColor.Color = ColorTranslator.FromHtml("#ff0000");
      for (Int32 r = 0; r < rows.Count(); ++r)
      {
         var label = chart.Series[c].DataLabels.Add();
         label.LabelValueVisible = true;
         label.Position = ChartDataLabelPosition.OutsideEnd;
      }
   }
}


The code results in the picture shown below, where the labels for the categories is overlapping the bars and values. I have three questions:

1) What can I do to get the category labels to the left of the chart?
2) How can I custom format the value labels to a preset amount of precision (number of decimals)?
3) The negative values are white instead of colored as the others. How do I get the series color to show?

sparticus1701
 
Posts: 5
Joined: Fri Jun 27, 2014 1:18 pm

Tue Feb 23, 2016 6:39 am

Hi,

Thanks for your posting.
1) What can I do to get the category labels to the left of the chart?
Please set
Code: Select all
 chart.PrimaryCategoryAxis.TickLabelPosition = TickLabelPositionType.TickLabelPositionLow;

2) How can I custom format the value labels to a preset amount of precision (number of decimals)?
Please try the following code, for example, number of decimals is 2.
Code: Select all
 double value = 0.00;
            for (Int32 r = 0; r < rows.Count(); ++r)
            {
                for (Int32 c = 0; c < cols.Count(); ++c)
                {
                  value=Math.Round(values[r,c],2);
                  chart.ChartData[r + 1, c + 1].Value = value;
                }
            }

3) The negative values are white instead of colored as the others. How do I get the series color to show?
Please set
Code: Select all
 chart.Series[c].InvertIfNegative = false;


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Tue Feb 23, 2016 2:35 pm

Thank you, those suggestions were helpful!

sparticus1701
 
Posts: 5
Joined: Fri Jun 27, 2014 1:18 pm

Wed Feb 24, 2016 2:07 am

Hi,

I am glad that your issues have been resolved.
Welcome to write to us again if you have further problems.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Presentation