c# - How to group data on the YAxis of a .NET chart? -
i have .net chart looks this:
here code used generate it:
protected void page_load(object sender, eventargs e) { // set chart properties ganttchart.width = 800; ganttchart.height = 500; // create chart area var area = new chartarea(); ganttchart.chartareas.add(area); ganttchart.chartareas[0].axisy.labelstyle.angle = 30; // unknown reason y-axis x-axis ganttchart.chartareas[0].axisy.labelstyle.format = "hh:mm dd/mmm";; // set data var dt = new datatable(); dt.columns.add("name", typeof(string)); dt.columns.add("seriesid", typeof(double)); dt.columns.add("y", typeof(datetime)); dt.columns.add("y2", typeof(datetime)); dt.rows.add("a", 1, new datetime(2015, 10, 22, 8, 0, 0), new datetime(2015, 10, 22, 9, 0, 0)); dt.rows.add("a", 1, new datetime(2015, 10, 22, 10, 0, 0), new datetime(2015, 10, 22, 12, 0, 0)); dt.rows.add("a", 1, new datetime(2015, 10, 22, 18, 0, 0), new datetime(2015, 10, 22, 20, 0, 0)); dt.rows.add("b", 2, new datetime(2015, 10, 22, 8, 30, 0), new datetime(2015, 10, 22, 9, 13, 0)); dt.rows.add("b", 2, new datetime(2015, 10, 22, 17, 0, 0), new datetime(2015, 10, 22, 18, 43, 0)); dt.rows.add("c", 3, new datetime(2015, 10, 22, 13, 0, 0), new datetime(2015, 10, 22, 14, 0, 0)); dt.rows.add("c", 3, new datetime(2015, 10, 22, 14, 22, 0), new datetime(2015, 10, 22, 14, 32, 0)); var series = new series(); series.name = "series1"; series.charttype = seriescharttype.rangebar; series.yvaluetype = chartvaluetype.datetime; // add series chart ganttchart.series.add(series); ganttchart.series[series.name].points.databind(dt.select(), "seriesid", "y,y2", "label=name"); ganttchart.series[series.name].label = "y = name"; }
what want achieve name field of datatable label on y-axis, along left hand side of chart see a, b & c instead of 0,1,2,3,4.
if change databind method use name instead of series id affect i'm after loose y-axis grouping. ends looking this:
what need happy medium grouping first picture , labels second. how can achieve this?
the solution problem not rely on databinding , instead create each point manually , add series myself. way had more control on properties of each point.
i made method called mapdatarowtodatapoint looked this:
private datapoint mapdatarowtodatapoint(datarow row) { try{ var point = new datapoint(); point.xvalue = double.parse(row["seriesid"].tostring()); point.yvalues = new double[]{ convert.todatetime(row["y1"].tostring()).tooadate(), convert.todatetime(row["y2"].tostring()).tooadate() }; point.axislabel = row["seriesname"].tostring(); point.color = colortranslator.fromhtml("#" + row["colour"].tostring()); point.borderwidth = 1; point.bordercolor = color.black; point.tooltip = row["tooltip"].tostring() + ((!string.isnullorempty(row["tooltip"].tostring()))?"\n":"") + "start date time: #valy{"+tooltipdatetimeformat+"}\n" +"end date time: #valy2{"+tooltipdatetimeformat+"}"; point.isvalueshownaslabel = false; point.postbackvalue = row["postbackvalue"].tostring(); return point; } catch { //log error return null; } }
here can see map row of datatable datapoint object , can control each property of data point in great detail.
now, instead of databinding, loop through each row of data source , call above method, add point chart's series:
// each row of datasource, create data point , add chart foreach(datarow row in data.rows) { var point = mapdatarowtodatapoint(row); if(point != null) ganttchart.series[0].points.add(point); }
this method proved more painless relying on data binding. hope helps!
Comments
Post a Comment