4

I have an a chart control which measures customer satisfaction.

The possible values are Happy, Neutral and Sad. (e.g. a possible set of values might be: Happy (23), Neutral (12), Sad (19) )

I want Happy to always be Green, Neutral to always be Orange and Sad to always be Red (e.g. like a RAG status)

I set the colors thus:

fbChart.Palette = ChartColorPalette.None;
fbChart.PaletteCustomColors = new Color[] { Color.Green, Color.Orange, Color.Red };

This works fine if all three possible options have values, but if there is only one or two with values the colors screws up. (e.g. If I have Happy (12) and Sad (9) but no Neutral, the colors come out as Green for Happy and Orange for Sad.)

Is there a way I can tie down the colors to the name (e.g Happy / Neutral / Sad)?

EDIT:

The data is loaded into the chart from a datatable:

    var fb = from f in db.tl_feedbacks
             where f.timestamp >= new DateTime(fromYear, fromMonth, fromDay, 0, 0, 0) &&                f.timestamp <= new DateTime(toYear, toMonth, toDay, 23, 59, 59)  
               group f by f.tl_feedback_score.score into grp
               select new
               {
                     Score = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(grp.Key),  
                     Count = grp.Select(x => x.tl_feedback_score.score).Count()  
               };  
              fbChart.Series["Feedback"].XValueMember = "Score";  
              fbChart.Series["Feedback"].YValueMembers = "Count";

              fbChart.DataSource = fb;
              fbChart.DataBind();
3
  • how did you bind your data to the charts ....through datatable ?
    – Glory Raj
    Oct 10, 2011 at 20:27
  • @errorstacks - yes, see above for code (added it as an edit)
    – Ben
    Oct 10, 2011 at 20:33
  • if that code is not working then pls let me know....
    – Glory Raj
    Oct 10, 2011 at 20:52

1 Answer 1

5

I am assuming that you are binding data to the charts using datatable and you are using asp.net chart controls......

I am hoping that this solution will helps you

foreach (Series charts in fbchart.Series)
{
    foreach (DataPoint point in charts.Points)
    {
       switch (point.AxisLabel)
        {
           case "Neutral": point.Color = Color.Red; break;
           case "Happy": point.Color = Color.Green; break;
           case "Sad": point.Color = Color.Orange; break;
         }
         point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);

    }
 }  

and the example image(chart) for the above code is like this...Belowdividing areas with membership types using pie chart controls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.