c# - MSChart : How to add custom label in bar chart -
i want add custom label ( category 1 , category 2 ) not getting idea whether need add new series or using custom label function.. appreciated.
here data table,
datatable dt = new datatable(); datacolumn dc; dc = new datacolumn(); dc.columnname = "question"; dt.columns.add(dc); dc = new datacolumn(); dc.columnname = "section"; dt.columns.add(dc); dc = new datacolumn(); dc.columnname = "user1"; dt.columns.add(dc); dc = new datacolumn(); dc.columnname = "user2"; dt.columns.add(dc); datarow dr; dr = dt.newrow(); dr["question"] = "a"; dr["section"] = "category 1"; dr["user1"] = "1"; dr["user2"] = "2"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "b"; dr["section"] = "category 1"; dr["user1"] = "4"; dr["user2"] = "5"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "c"; dr["section"] = "category 1"; dr["user1"] = "2"; dr["user2"] = "5"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "x"; dr["section"] = "category 2"; dr["user1"] = "3"; dr["user2"] = "5"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "y"; dr["section"] = "category 2"; dr["user1"] = "5"; dr["user2"] = "1"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "z"; dr["section"] = "category 2"; dr["user1"] = "6"; dr["user2"] = "5"; dt.rows.add(dr);
my chart plotting code,
series series = new series("series1"); chart4.series.add("series1"); chart4.series.add("series2"); chart4.chartareas.add("chartarea1"); // set series chart type chart4.series["series1"].charttype = seriescharttype.bar; chart4.series["series2"].charttype = seriescharttype.bar; // draw 3d cylinder chart4.series["series1"]["drawingstyle"] = "cylinder"; chart4.series["series2"]["drawingstyle"] = "cylinder"; chart4.datasource = dt.defaultview; chart4.chartareas["chartarea1"].axisx.interval = 1; chart4.series["series1"].xvaluemember = "question"; chart4.series["series1"].yvaluemembers = "user1"; chart4.series["series2"].yvaluemembers = "user2"; chart4.databind();
solution
datatable dt = new datatable(); datacolumn dc; dt.columns.add("id", typeof(int)); dc = new datacolumn(); dc.columnname = "question"; dt.columns.add(dc); dc = new datacolumn(); dc.columnname = "section"; dt.columns.add(dc); dc = new datacolumn(); dc.columnname = "user1"; dt.columns.add(dc); datarow dr; dr = dt.newrow(); dr["question"] = "aa"; dr["id"] = 1; dr["section"] = "category1"; dr["user1"] = "6"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "bb"; dr["id"] = 2; dr["section"] = "category1"; dr["user1"] = "1"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "xx"; dr["id"] = 3; dr["section"] = "category2"; dr["user1"] = "3"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "yy"; dr["id"] = 4; dr["section"] = "category2"; dr["user1"] = "4"; dt.rows.add(dr); dr = dt.newrow(); dr["question"] = "zz"; dr["id"] = 5; dr["section"] = "category2"; dr["user1"] = "2"; dt.rows.add(dr); series series = new series("series1"); chart4.series.add("series1"); chart4.chartareas.add("chartarea1"); chart4.series["series1"].charttype = seriescharttype.bar; chart4.series["series1"]["barlabelstyle"] = "center"; chart4.series["series1"]["drawingstyle"] = "cylinder"; chart4.series["series1"].xvaluemember = "question"; chart4.series["series1"].yvaluemembers = "user1"; chart4.datasource = dt; chart4.databind(); foreach (var g in dt.asenumerable().groupby(x => x.field<string>("section"))) { string section = g.key; var questions = g.select(r => new { id = r.field<int>("id"), question = r.field<string>("question") }); int min = questions.min(y => y.id); int max = questions.max(y => y.id); var sectionlabel=new customlabel(); if (min == max) { sectionlabel = new customlabel(min - 0.5, max + 0.5, section, 1, labelmarkstyle.linesidemark); } else { sectionlabel = new customlabel(min, max, section, 1, labelmarkstyle.linesidemark); } chart4.chartareas[0].axisx.customlabels.add(sectionlabel); } chart4.chartareas["chartarea1"].axisx.interval = 1;
the easiest way add annotations chart
textannotation text = new textannotation(); text.text = 'category 1'; text.x = 20; text.y = 20; chart4.annotations.add(text);
you can draw line using lineannotation
Comments
Post a Comment