|
ArealLayer problem |
Posted by Don_kaotic on Nov-29-2013 04:35 |
|
Hello!
I changed the realtime chart and I writed a mousewheel listener to display mouse wheel
rotation on real time with an area layer (it works for me but with only one color)but my
problem is that i want the area layer to be
gradient in " green --> red " when the value of rotation is >0 and red --> green when the
value <0 like in the picture
Here's my code
For the mousewheel listener:
do
{
chartViewer1.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
Integer rx = e.getWheelRotation() ;
Wheel = Wheel-rx ;
//Integer dataA=new Integer(Integer.parseInt(valueA.getText())-rx) ;
}
});
// Now we shift the data into the array
shiftData(dataSeriesA, Wheel);
shiftData(timeStamps, nextDataTime);
// Update nextDataTime
nextDataTime = new Date(nextDataTime.getTime() + dataInterval);;
}
while (nextDataTime.before(now));
And this for the area layer:
// Create a line AreaLayer to plot the lines
AreaLayer layer = c.addAreaLayer();
// The x-coordinates are the timeStamps.
layer.setXData(timeStamps);
// The 3 data series are used to draw 3 lines. Here we put the latest data
// values as part of the data set name, so you can see them updated in the
// legend box.
layer.addDataSet(dataSeriesA, 0x6080ff80, "Alpha: <*bgColor=FFCCCC*>" +
c.formatValue(dataSeriesA[dataSeriesA.length - 1], " {value|2} "));
|
Re: ArealLayer problem |
Posted by Peter Kwan on Nov-30-2013 00:32 |
|
Hi Don_kaotic,
You may use a "y-zone color" as the area color. This allows you to specify two colors to fill the area based on the y-value (see the "Y Zone Coloring" sample code included in ChartDirector). You can set the two colors to two different graident colors to achieve the effect you want. The code is lilke:
// ensure the y = 0 line is at the center of the plot area
c.setAxisAtOrigin(0, Chart.YAxisSymmetric);
// create the gradient for the region above the center of the plot area
int topColor = c.linearGradientColor(0, c.getPlotArea().getTopY(), 0, c.getPlotArea().getTopY() + c.getPlotArea().getHeight() / 2, 0x7f00ff00, unchecked((int)0xef00ff00));
// create the gradient for the region below the center of the plot area
int bottomColor = c.linearGradientColor(0, c.getPlotArea().getBottomY(), 0, c.getPlotArea().getTopY() + c.getPlotArea().getHeight() / 2, 0x7fff0000, unchecked((int)0xefff0000));
// define a zone color that chooses between the bottomColor and topColor depending
// whether y >= 0 or not
int zoneColor = c.yZoneColor(0, bottomColor, topColor);
// Use the zone color as the area fill color
c.addAreaLayer(data, areaColor);
Note that to define the gradients, we need to know where the gradient start (that is, where is the y = 0 line). If we use "c.setAxisAtOrigin(0, Chart.YAxisSymmetric);" to ensure the y = 0 line is at the center of the plot area, then it is easy to know where does the gradient start. If you do not use that line, ChartDirector can put the y = 0 line at other location based on your data. In this case, your code can create an area layer with some dummy color first. Then it can call c.layoutAxes() to ask ChartDirector to auto-scale the axis based on the data in the layer. In this way, you can use XYChart.getYCoor to determine where is the y = 0 position, and create the gradient and zone colors. After that, you may use myAreaLayer.getDataSet(0).setDataColor(...) to update the area fill color to be the zone color.
Hope this can help.
Regards
Peter Kwan |
Re: ArealLayer problem |
Posted by Don_kaotic on Nov-30-2013 01:50 |
|
Thank you Mr Peter,
I will try this. |
|