We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Change color of bar in bar chart based on condition

Hi Team,

We are using Aspose cells for .NET.

How to change a color of bar in bar chart based on it’s X value?

Can you please help us on this?


Thanks for your query.

Well, you may apply the fill color of bars using its relevant chart point/ data series in the Bar chart. See the sample code for your reference. The sample is only for demonstration purpose. Please refer to it and you got to write your own code based on your specific data and condition:
Sample code:

Workbook workbook = new Workbook();

//Change color palette a bit.

workbook.ChangePalette(Color.Orange, 53);

workbook.ChangePalette(Color.LightBlue, 54);

workbook.ChangePalette(Color.LightCoral, 55);

//Get the colors in the Workbook palette

Color[] colors = workbook.Colors;

//Set default font

Style style = workbook.DefaultStyle;

style.Font.Name = "Tahoma";

workbook.DefaultStyle = style;

Cells cells = workbook.Worksheets[0].Cells;

//Put a string into a cell





cells["B1"].PutValue("Marketing Costs");




Worksheet sheet = workbook.Worksheets[0];

sheet.IsGridlinesVisible = false;

Worksheet sheet1 = workbook.Worksheets[workbook.Worksheets.Add(SheetType.Worksheet)];

//Create chart

int chartIndex = sheet1.Charts.Add(ChartType.Bar, 5, 1, 29, 10);

Chart chart = sheet1.Charts[chartIndex];

//Add the nseries collection to a chart

chart.NSeries.Add("Sheet1!B2:B4", true);

//Get or set the range of category axis values

chart.NSeries.CategoryData = "Sheet1!A2:A4";

chart.NSeries.IsColorVaried = true;

chart.NSeries[0].DataLabels.ShowValue = true;

chart.NSeries[0].Line.Color = Color.Red;

chart.NSeries[0].Line.Style = LineType.Solid;

chart.NSeries[0].Line.Weight = WeightType.MediumLine;

//Chnage bar fill color for each point

for (int i = 0; i < chart.NSeries[0].Points.Count; i++)


chart.NSeries[0].Points[i].Area.ForegroundColor = colors[53 + i];


//Set properties of chart

chart.ChartArea.Border.IsVisible = false;

chart.ChartArea.Area.ForegroundColor = Color.Transparent;

//Set the legend position type

chart.Legend.Position = LegendPositionType.Top;

chart.GapWidth = int.Parse("100");

//Set properties of chart title

chart.Title.Text = "Marketing Costs by Region";

chart.Title.TextFont.IsBold = true;

chart.Title.TextFont.Color = Color.Black;

chart.Title.TextFont.Size = 12;

//Set properties of categoryaxis title

chart.CategoryAxis.Title.Text = "Region";

chart.CategoryAxis.Title.TextFont.Color = Color.Black;

chart.CategoryAxis.TickLabels.RotationAngle = 0;

//Set properties of valueaxis title

chart.ValueAxis.Title.Text = "In Thousands";

chart.ValueAxis.Title.TextFont.Name = "Arial";

chart.ValueAxis.Title.TextFont.Color = Color.Black;

chart.ValueAxis.Title.TextFont.IsBold = true;

chart.ValueAxis.Title.TextFont.Size = 10;

chart.ValueAxis.Title.RotationAngle = 90;

chart.ValueAxis.MajorUnit = double.Parse("20000");

chart.ValueAxis.MaxValue = double.Parse("80000");

chart.ValueAxis.MinorUnit = double.Parse("5000");

chart.ValueAxis.MinValue = double.Parse("0");


Hope, this helps a bit.

Thank you.


Thanks for reply.
How can we get the value of the x and y value in a bar chart? We need to
change the value of a bar If the x is a specific value.


Well, you may try to use ChartPoint/Series.XValue and ChartPoint/Series.YValue attributes to get the corresponding values from the data source of the chart. Moreover, you got to call Chart.Calculate() method before retrieving the above mentioned properties.

Hope, this helps a bit.

Thank you.


Thanks for reply, This helped us.


Good to know that it figures out your issue now. Feel free to write us back if you have further comments or questions, we will be happy to assist you soon.

Thank you.