What i want is to set borders between two series in StackedBar Like this image The bold black line between blue and green
I can not figure out any idea to specify the border, i tried to set the borders to the series throuh this code
chart.Series["series0"].BorderWidth = 2;
chart.Series["series0"].BorderColor = Color.Black;
chart.Series["series0"].BorderDashStyle = ChartDashStyle.Solid;
but this the result i got
Here's my code
double l = Convert.ToDouble(query1[i - 1][0]) - 10;
string n = query1[i - 1][1];
int count = 0;
for (double t = l; t < l + 10; t++)
{
//Next line Calc. the occurence of character in a text file
count = n.Split('C').Length - 1;
//Multiple the occurence by 10 so it become percent
chart.Series["series0"].Points.AddXY(t, count * 10);
chart.Series["series0"]["PointWidth"] = "1";
chart.Series["series0"].BorderWidth = 2;
chart.Series["series0"].BorderColor = Color.Black;
chart.Series["series0"].BorderDashStyle = ChartDashStyle.Solid;
count = n.Split('o').Length - 1;
chart.Series["series1"].Points.AddXY(t, count * 10);
chart.Series["series1"]["PointWidth"] = "1";
}
How to achieve the first pic effect using StackedBar ? , if i can not using StackedBar, what chart type you suggest to use ??
There are no built-in chart elements that could easily be made into a borderline between those two Series. (Creating
LineAnnotations
to achieve this would be a nightmare..)So the way to add the lines is to draw them onto the surface of the
Chart
. This is most naturally done in thePostPaint
event, provided just for such adornments.Here the
Axes
have handy functions to convert between the data values and the pixel positions. We need theValueToPixelPosition
method.I will take you through variations of
Chart
drawing that gradually get a little more complicated as we approach the final version..:Let's start with a simple example: Let's build and adorn a
StackedArea
chart; here is the drawing code:The
Points.Select
is really just a shorthand for a loop; so after creating the pixel point list we simply draw it.Now, as you can see, as
StackedArea
chart is pointy and doesn't look like aStackedBar
orStackedColumn
chart. So let's cheat and 'rectify' the area chart by adding a few extra points:Results:
Now that was not so hard; unfortunately you just can't turn a
StackedArea
to go from left to right instead of bottom-up. So we need to change the chart type to aBar
type eventually..Here the challenge is to find the right upper and lower corners of those bars. We do have the
DataPoint
values, but these are in the middle of the bars. So we need to add/subtract half of the Bars' width to get the corners. For this we need the width.While you have set it with the
PointWidth
property to1
, what we really need is the pixel width. We best get it by subtracting the pixel coordinates of two neighbouring points.This makes the
PostPaint
event a little longer, but still not overly complicated; we will start with aStackedColumn
chart, adding two corner points for each data point:Now this looks pretty much identical to our fake version of the 'rectified area chart'. What will we need to change to apply this to a
StackedBar
chart? Almost nothing! The only two things we need to take care of areBar
charts.Here are the two stacked charts with a border:
This is the loop for the
StackBar
chart:Note that I am drawing with a fixed pen width of 4 pixels. To make it scale with the
Chart
you may want to calculate the pen width dynamically..Update
To draw borders on top of several series you can put the code into a loop like this: