Home » Archived » BIRT » Drag and Drop function for Bird
|
Re: Drag and Drop function for Bird [message #756680 is a reply to message #756628] |
Mon, 14 November 2011 16:14 |
|
Peter,
Are you using the chart engine api to run the charts?
Jason
On 11/14/2011 9:30 AM, gloeckner.peter wrote:
> Hi,
> I'm trying to implement a drag and drop function for bird (rcp and rap)
> but there are a lot of problems i'm not able to solve.
>
> I'm able to print the pixel the user clicked on relativly to the point
> in the top left, but i'm not able to compute where the position is
> relativly to the origin of the graph. Therefore I need to know the size
> of the Title etc. or the Position of the ClientArea of the Graph but all
> methods like getWidth or getX don't provide the right values.
>
> On the other hand i have some problems with my triggers :) .
> For example i'm able to show a tooltip when a point is clicked (with the
> code below) but i have no idea, how to call a function which prints some
> text when a point is clicked.
>
> <code>
> bs1.getTriggers().add(TriggerImpl
> create(TriggerCondition.ONMOUSEDOWN_LITERAL, ActionImpl
> create(ActionType.SHOW_TOOLTIP_LITERAL,
> TooltipValueImpl.create(500, null))));
> </code>
>
> Thanks for help,
> Peter
|
|
| |
Re: Drag and Drop function for Bird [message #756757 is a reply to message #756699] |
Mon, 14 November 2011 23:13 |
|
I am not sure this will help but you can define your own action renderer
for the chart engine like:
gcs = gr.build(dServer, cm, bo, null, null, null);
gcs.getRunTimeContext().setActionRenderer( new MyActionRenderer());
dRenderer.setProperty(IDeviceRenderer.UPDATE_NOTIFIER,
new EmptyUpdateNotifier(cm, gcs.getChartModel()));
Then the action render should extend the ActionRendererAdapter class like:
import org.eclipse.birt.chart.render.ActionRendererAdapter;
import org.eclipse.birt.chart.computation.DataPointHints;
import org.eclipse.birt.chart.event.StructureSource;
import org.eclipse.birt.chart.event.StructureType;
import org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator;
import org.eclipse.birt.chart.model.attribute.ActionType;
import org.eclipse.birt.chart.model.attribute.TooltipValue;
import org.eclipse.birt.chart.model.attribute.URLValue;
import org.eclipse.birt.chart.model.data.Action;
import org.eclipse.birt.chart.render.ActionRendererAdapter;
import org.eclipse.birt.chart.util.ChartUtil;
/**
* Simple implementation for IActionRenderer
*/
public class MyActionRenderer extends ActionRendererAdapter {
public void processAction( Action action, StructureSource source )
{
if ( ActionType.SHOW_TOOLTIP_LITERAL.equals( action.getType( ) ) )
{
TooltipValue tv = (TooltipValue) action.getValue( );
if ( StructureType.SERIES_DATA_POINT.equals( source.getType( ) ) )
{
final DataPointHints dph = (DataPointHints) source.getSource( );
String MyToolTip = "My Value is " + dph.getDisplayValue() + "--"
+dph.getBaseDisplayValue();
tv.setText( MyToolTip );
}
}
}
}
Jason
On 11/14/2011 12:39 PM, gloeckner.peter wrote:
> Yes, I'm using the chart engine.
|
|
| |
Re: Drag and Drop function for Bird [message #756943 is a reply to message #756884] |
Tue, 15 November 2011 16:07 |
|
I know the chart engine when used within a report can invoke a client
side script when a data point is clicked. I believe you may be able to
setup the chart engine to do this in your app. If you had a simple app
that shows the problem that you could share, we may be able to help.
Jason
On 11/15/2011 7:37 AM, gloeckner.peter wrote:
> Unfortunately this doesn't help. I've allready implemented a
> ActionRenderer. The problem is that the method processAction isn't
> called wenn a action happens (for example when i am clicking on a data
> point), it's called consistently.
>
> Thanks
> Peter
|
|
|
Re: Drag and Drop function for Bird [message #757005 is a reply to message #756943] |
Tue, 15 November 2011 23:07 |
gloeckner.peter Messages: 6 Registered: November 2011 |
Junior Member |
|
|
Hi,
This are my two classes.
The class SwtLiveChartViewer is copied from a tutorial.
In the class LineChart i am adding the trigger.
bs1.getTriggers().add(TriggerImpl
.create(TriggerCondition.ONMOUSEDOWN_LITERAL, ActionImpl
.create(ActionType.SHOW_TOOLTIP_LITERAL,
TooltipValueImpl.create(500, null))));
When i'm clicking on a point the tooltipp is displayed. In fact i only want to call a function, which i can defined, when the user clicks on a point.
Thanks
Peter
package org.eclipse.ui.tutorials.rcp.charts;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.ActionType;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.DataPoint;
import org.eclipse.birt.chart.model.attribute.DataPointComponentType;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.LineStyle;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.TriggerCondition;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.attribute.impl.DataPointComponentImpl;
import org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl;
import org.eclipse.birt.chart.model.attribute.impl.TooltipValueImpl;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
import org.eclipse.birt.chart.model.data.NumberDataSet;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.impl.ActionImpl;
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.data.impl.TriggerImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
import org.eclipse.birt.chart.model.type.LineSeries;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.swt.widgets.Composite;
public class LineChart extends SwtLiveChartViewer{
public void init(){
xData = new double[2];
yData = new double[2];
xData[0] = 1;
xData[1] = 2;
yData[0] = 1;
yData[1] = 2;
}
public double[] xData;
public double[] yData;
/**
* std - Konstruktor
* @param parent
* @param style
*/
public LineChart(Composite parent, int style) {
super(parent, style);
}
/**
* Chart Eigenschaften festlegen
*/
public Chart createChart(){
ChartWithAxes chart = ChartWithAxesImpl.create();
//x-Achse
Axis xAxis = chart.getPrimaryBaseAxes()[0];
xAxis.getTitle().setVisible(false); //Titel
xAxis.getTitle().getCaption().setValue("x");
xAxis.getTitle().getCaption().getFont().setBold(false);
xAxis.getTitle().getCaption().getFont().setSize(11);
xAxis.getTitle().getCaption().getFont().setName("MS Sans Serif");
xAxis.getLabel().setVisible(false); //Achsenbeschriftung
xAxis.getLabel().getCaption().getFont().setSize(20);
xAxis.getLabel().getCaption().getFont().setName("MS Sans Serif");
xAxis.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
xAxis.setType(AxisType.LINEAR_LITERAL);
xAxis.getOrigin().setType(IntersectionType.VALUE_LITERAL);
xAxis.getScale().setStep(1.0);
//y-Achse
Axis yAxis = ((ChartWithAxes) chart).getPrimaryOrthogonalAxis(xAxis);
yAxis.getTitle().setVisible(false);
yAxis.getTitle().getCaption().setValue("y");
yAxis.getTitle().getCaption().getFont().setBold(false);
yAxis.getTitle().getCaption().getFont().setRotation(90);
yAxis.getTitle().getCaption().getFont().setSize(11);
yAxis.getTitle().getCaption().getFont().setName("MS Sans Serif");
yAxis.getLabel().setVisible(false);
yAxis.getLabel().getCaption().getFont().setSize(8);
yAxis.getLabel().getCaption().getFont().setName("MS Sans Serif");
yAxis.getMajorGrid().getLineAttributes().setVisible(true);
yAxis.getMajorGrid().getLineAttributes().setColor(ColorDefinitionImpl.GREY());
yAxis.getMajorGrid().getLineAttributes().setStyle(LineStyle.DASHED_LITERAL);
yAxis.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
yAxis.setType(AxisType.LINEAR_LITERAL);
yAxis.getOrigin().setType(IntersectionType.VALUE_LITERAL);
yAxis.getScale().setStep(1.0);
//TODO
init();
//x-Series
Series xSeries = SeriesImpl.create();
SeriesDefinition sdX = SeriesDefinitionImpl.create();
sdX.getSeries().add(xSeries);
xAxis.getSeriesDefinitions().add(sdX);
// X-Axis
NumberDataSet dataX = NumberDataSetImpl.create( xData );
xSeries.setDataSet(dataX);
NumberDataSet orthoValuesDataSet1 = NumberDataSetImpl.create(yData);
LineSeries bs1 = (LineSeries) LineSeriesImpl.create();
bs1.setDataSet(orthoValuesDataSet1);
//bs1.setRiserOutline(null);
DataPoint dp = bs1.getDataPoint();
dp.getComponents().clear();
dp.setPrefix("(");
dp.setSuffix(")");
// Chart doesn't support format specifier for text, so we use
// JavaNumberFormatSpecifierImpl here.
dp.getComponents().add(DataPointComponentImpl
.create(DataPointComponentType.BASE_VALUE_LITERAL,
JavaNumberFormatSpecifierImpl.create("0")));
dp.getComponents().add(DataPointComponentImpl
.create(DataPointComponentType.ORTHOGONAL_VALUE_LITERAL,
JavaNumberFormatSpecifierImpl.create("0")));
bs1.getTriggers().add(TriggerImpl
.create(TriggerCondition.ONMOUSEDOWN_LITERAL, ActionImpl
.create(ActionType.SHOW_TOOLTIP_LITERAL,
TooltipValueImpl.create(500, null))));
SeriesDefinition sdY = SeriesDefinitionImpl.create();
yAxis.getSeriesDefinitions().add(sdY);
sdY.getSeries().add(bs1);
return chart;
}
/**
*
* @param chart
*/
public void updateDataSet(ChartWithAxes chart)
{
Axis xAxis = chart.getPrimaryBaseAxes()[0];
Axis yAxis = chart.getPrimaryOrthogonalAxis(xAxis);
NumberDataSet dataX = NumberDataSetImpl.create( xData );
NumberDataSet dataY = NumberDataSetImpl.create( yData );
// X-Axis
SeriesDefinition sdX = (SeriesDefinition) xAxis.getSeriesDefinitions().get(0);
((Series) sdX.getSeries().get(0)).setDataSet(dataX);
// Y-Axis
SeriesDefinition sdY = (SeriesDefinition) yAxis.getSeriesDefinitions().get(0);
((Series) sdY.getSeries().get(0)).setDataSet(dataY);
}
public void insertPoint(String x, String y){
System.out.println("DoubleKLick: " + x + " " + y);
double[] xDataNew = new double[xData.length + 1];
double[] yDataNew = new double[yData.length + 1];
System.arraycopy(xData, 0, xDataNew, 0, xData.length);
System.arraycopy(yData, 0, yDataNew, 0, yData.length);
xDataNew[xData.length] = Double.valueOf(x);
yDataNew[yData.length] = Double.valueOf(y);
xData = xDataNew;
yData = yDataNew;
updateDataSet((ChartWithAxes) chart);
}
@Override
public Chart getDesignTimeModel() {
return gcs.getChartModel();
}
@Override
public Chart getRunTimeModel() {
return chart;
}
@Override
public Object peerInstance() {
return this;
}
@Override
public void regenerateChart() {
redraw();
}
@Override
public void repaintChart() {
redraw();
}
}
//
package org.eclipse.ui.tutorials.rcp.charts;
import org.eclipse.birt.chart.api.ChartEngine;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.device.IUpdateNotifier;
import org.eclipse.birt.chart.exception.ChartException;
import org.eclipse.birt.chart.factory.GeneratedChartState;
import org.eclipse.birt.chart.factory.Generator;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.attribute.Bounds;
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
import org.eclipse.birt.core.framework.PlatformConfig;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
public abstract class SwtLiveChartViewer extends Composite implements PaintListener, ControlListener, IUpdateNotifier{
private IDeviceRenderer idr = null;
public Chart chart;
public GeneratedChartState gcs = null;
/**
* Used in building the chart for the first time
*/
private boolean bFirstPaint = true;
/**
* Constructor
*/
public SwtLiveChartViewer( Composite parent, int style )
{
super( parent, style );
try
{
PlatformConfig config = new PlatformConfig( );
config.setBIRTHome("");
idr = ChartEngine.instance( config ).getRenderer( "dv.SWT" );
idr.setProperty(IDeviceRenderer.UPDATE_NOTIFIER, this);
}
catch ( ChartException pex )
{
pex.printStackTrace();
}
chart = createChart( );
}
@Override
public void dispose() {
if( (imgChart !=null) && (!imgChart.isDisposed()))imgChart.dispose();
if( (gcImage !=null) && (!gcImage.isDisposed()))gcImage.dispose();
super.dispose();
}
public abstract Chart createChart();
private Image imgChart;
private GC gcImage;
private Bounds bo;
/*
* (non-Javadoc)
*
* @see
* org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events
* .PaintEvent)
*/
public final void paintControl( PaintEvent e )
{
Rectangle d = this.getClientArea( );
if ( bFirstPaint )
{
//if resized
if( (imgChart !=null) && (!imgChart.isDisposed()))imgChart.dispose();
if( (gcImage !=null) && (!gcImage.isDisposed()))gcImage.dispose();
imgChart = new Image( this.getDisplay( ), d );
gcImage = new GC( imgChart );
idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
bo = BoundsImpl.create( 0, 0, d.width, d.height );
bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
}
Generator gr = Generator.instance( );
try
{
gcs = gr.build( idr.getDisplayServer( ), chart, bo, null, null, null );
// gcs.getRunTimeContext().setActionRenderer( new MyActionHandler());
gr.render( idr, gcs );
GC gc = e.gc;
gc.drawImage( imgChart, d.x, d.y );
}
catch ( ChartException ce )
{
ce.printStackTrace( );
}
bFirstPaint = false;
Display.getDefault( ).timerExec( 500, new Runnable( ) {
public void run( )
{
chartRefresh( );
}
} );
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
* .swt.events.SelectionEvent)
*/
private void chartRefresh( )
{
if ( !isDisposed( ) )
{
final Generator gr = Generator.instance( );
//scrollData( (ChartWithAxes) chart );
// Refresh
try
{
gr.refresh( gcs );
}
catch ( ChartException ex )
{
ex.printStackTrace( );
}
redraw( );
}
}
public void controlMoved(ControlEvent arg0) {
bFirstPaint = true;
}
public void controlResized(ControlEvent arg0) {
bFirstPaint = true;
}
}
|
|
| |
Re: Drag and Drop function for Bird [message #757820 is a reply to message #757005] |
Wed, 16 November 2011 16:35 |
|
Your class needs to implement the
ICallBackNotifier. Take a look at this example.
package my.swt.live.viewer;
import org.eclipse.birt.chart.computation.DataPointHints;
import org.eclipse.birt.chart.device.ICallBackNotifier;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.event.StructureSource;
import org.eclipse.birt.chart.event.WrappedStructureSource;
import org.eclipse.birt.chart.exception.ChartException;
import org.eclipse.birt.chart.factory.GeneratedChartState;
import org.eclipse.birt.chart.factory.Generator;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.ActionType;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.Bounds;
import org.eclipse.birt.chart.model.attribute.CallBackValue;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.LineAttributes;
import org.eclipse.birt.chart.model.attribute.LineStyle;
import org.eclipse.birt.chart.model.attribute.Marker;
import org.eclipse.birt.chart.model.attribute.MarkerType;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.RiserType;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.TriggerCondition;
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
import org.eclipse.birt.chart.model.attribute.impl.CallBackValueImpl;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
import org.eclipse.birt.chart.model.data.NumberDataSet;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.TextDataSet;
import org.eclipse.birt.chart.model.data.impl.ActionImpl;
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.TriggerImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
import org.eclipse.birt.chart.model.layout.Legend;
import org.eclipse.birt.chart.model.layout.Plot;
import org.eclipse.birt.chart.model.type.BarSeries;
import org.eclipse.birt.chart.model.type.LineSeries;
import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.birt.chart.util.PluginSettings;
import org.eclipse.birt.core.framework.PlatformConfig;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class SwtLiveChartViewer extends Composite implements
PaintListener,
ICallBackNotifier
{
private boolean bNeedsGeneration = true;
private IDeviceRenderer idr = null;
private Chart cm = null;
private GeneratedChartState gcs = null;
public static SwtLiveChartViewer c3dViewer;
/**
* Used in building the chart for the first time
*/
private boolean bFirstPaint = true;
public Chart getDesignTimeModel( )
{
return cm;
}
public void regenerateChart( )
{
redraw( );
}
public Chart getRunTimeModel( )
{
return gcs.getChartModel( );
}
public void repaintChart( )
{
redraw( );
}
public Object peerInstance()
{
return this;
}
public void callback( Object event, Object source, CallBackValue value )
{
Object obj1 =
((DataPointHints)((WrappedStructureSource)source).getSource()).getBaseValue();
Object obj2 =
((DataPointHints)((WrappedStructureSource)source).getSource()).getOrthogonalValue().toString();
Object obj3 =
((DataPointHints)((WrappedStructureSource)source).getSource()).getSeriesValue();
double xval =
((DataPointHints)((WrappedStructureSource)source).getSource()).getLocation().getX();
double yval =
((DataPointHints)((WrappedStructureSource)source).getSource()).getLocation().getY();
StructureSource structuredSource = (StructureSource) source;
DataPointHints dph = (DataPointHints) structuredSource.getSource();
Object obj4 =
((DataPointHints)((WrappedStructureSource)source).getSource()).getSeriesValue();
MessageBox mb = new MessageBox ( this.getShell( ) );
mb.setMessage( xval + "," + yval);
mb.open( );
}
/**
* execute application
*
* @param args
*/
public static void main( String[] args )
{
Display display = Display.getDefault( );
Shell shell = new Shell( display );
shell.setSize( 600, 400 );
shell.setLayout( new GridLayout( ) );
c3dViewer = new SwtLiveChartViewer( shell, SWT.NO_BACKGROUND );
c3dViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) );
c3dViewer.addPaintListener( c3dViewer );
shell.open( );
while ( !shell.isDisposed( ) )
{
if ( !display.readAndDispatch( ) )
display.sleep( );
}
display.dispose( );
}
/**
* Constructor
*/
SwtLiveChartViewer( Composite parent, int style )
{
super( parent, style );
PlatformConfig config = new PlatformConfig();
config.setProperty( "STANDALONE", "true" ); //$NON-NLS-1$//$NON-NLS-2$
final PluginSettings ps = PluginSettings.instance( config );
try
{
idr = ps.getDevice( "dv.SWT" );//$NON-NLS-1$
idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );
}
catch ( ChartException ex )
{
ex.printStackTrace( );
}
cm = createLiveChart( );
}
public static final Chart createLiveChart( )
{
ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
//Plot
cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
Plot p = cwaBar.getPlot( );
p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255,
255,
225 ) );
//Legend
Legend lg = cwaBar.getLegend( );
LineAttributes lia = lg.getOutline( );
lg.getText( ).getFont( ).setSize( 16 );
lia.setStyle( LineStyle.SOLID_LITERAL );
lg.getInsets( ).setLeft( 10 );
lg.getInsets( ).setRight( 10 );
//Title
cwaBar.getTitle( )
.getLabel( )
.getCaption( )
.setValue( "Live Chart Demo" );//$NON-NLS-1$
//X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
xAxisPrimary.setType( AxisType.TEXT_LITERAL );
xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL );
xAxisPrimary.getOrigin( ).setType( IntersectionType.MIN_LITERAL );
xAxisPrimary.getTitle( )
.getCaption( )
.setValue( "Category Text X-Axis" );//$NON-NLS-1$
xAxisPrimary.setTitlePosition( Position.BELOW_LITERAL );
xAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 75 );
xAxisPrimary.setLabelPosition( Position.BELOW_LITERAL );
xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
xAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setStyle( LineStyle.DOTTED_LITERAL );
xAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setColor( ColorDefinitionImpl.create( 64, 64, 64 ) );
xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true );
//Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
yAxisPrimary.getLabel( ).getCaption( ).setValue( "Price Axis"
);//$NON-NLS-1$
yAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 37 );
yAxisPrimary.setLabelPosition( Position.LEFT_LITERAL );
yAxisPrimary.setTitlePosition( Position.LEFT_LITERAL );
yAxisPrimary.getTitle( ).getCaption( ).setValue( "Linear Value Y-Axis"
);//$NON-NLS-1$
yAxisPrimary.setType( AxisType.LINEAR_LITERAL );
yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
yAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setStyle( LineStyle.DOTTED_LITERAL );
yAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setColor( ColorDefinitionImpl.RED( ) );
yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true );
//X-Series
Series seCategory = SeriesImpl.create( );
SeriesDefinition sdX = SeriesDefinitionImpl.create( );
xAxisPrimary.getSeriesDefinitions( ).add( sdX );
sdX.getSeries( ).add( seCategory );
//Y-Series (1)
BarSeries bs1 = (BarSeries) BarSeriesImpl.create( );
bs1.setSeriesIdentifier( "Unit Price" );//$NON-NLS-1$
bs1.setRiserOutline( null );
bs1.setRiser( RiserType.RECTANGLE_LITERAL );
//bs1.getTriggers( ).add(
// TriggerImpl.create( TriggerCondition.MOUSE_CLICK_LITERAL,
// ActionImpl.create( ActionType.TOGGLE_VISIBILITY_LITERAL,
SeriesValueImpl.create("not used" ) ) ) );
//Trigger tr1 = TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,
// ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL,
// TooltipValueImpl.create(200, null)));
bs1.getTriggers( ).add( TriggerImpl.create(
TriggerCondition.ONMOUSEOVER_LITERAL,
ActionImpl.create( ActionType.CALL_BACK_LITERAL,
CallBackValueImpl.create( String.valueOf( bs1.getSeriesIdentifier(
) ) ) ) ) );
//bs1.getTriggers().add(tr1);
//Y-Series (2)
LineSeries ls1 = (LineSeries) LineSeriesImpl.create( );
ls1.setSeriesIdentifier( "Quantity" );//$NON-NLS-1$
ls1.getLineAttributes( ).setColor( ColorDefinitionImpl.GREEN( ) );
for ( int i = 0; i < ls1.getMarkers( ).size( ); i++ )
{
( (Marker) ls1.getMarkers( ).get( i ) ).setType(
MarkerType.BOX_LITERAL );
}
ls1.setCurve( true );
SeriesDefinition sdY = SeriesDefinitionImpl.create( );
yAxisPrimary.getSeriesDefinitions( ).add( sdY );
sdY.getSeriesPalette( ).shift( -1 );
sdY.getSeries( ).add( bs1 );
sdY.getSeries( ).add( ls1 );
// Update data
updateDataSet( cwaBar );
return cwaBar;
}
static final void updateDataSet( ChartWithAxes cwaBar )
{
// Associate with Data Set
TextDataSet categoryValues = TextDataSetImpl.create( sa );
NumberDataSet seriesOneValues = NumberDataSetImpl.create( da1 );
NumberDataSet seriesTwoValues = NumberDataSetImpl.create( da2 );
// X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
SeriesDefinition sdX = (SeriesDefinition)
xAxisPrimary.getSeriesDefinitions( )
.get( 0 );
( (Series) sdX.getSeries( ).get( 0 ) ).setDataSet( categoryValues );
// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
SeriesDefinition sdY = (SeriesDefinition)
yAxisPrimary.getSeriesDefinitions( )
.get( 0 );
( (Series) sdY.getSeries( ).get( 0 ) ).setDataSet( seriesOneValues );
( (Series) sdY.getSeries( ).get( 1 ) ).setDataSet( seriesTwoValues );
}
//Live Date Set
private static final String[] sa = {
"One", "Two", "Three", "Four",
"Five",//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
"Six", "Seven", "Eight", "Nine",
"Ten"};//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
private static final double[] da1 = {
56.99,
352.95,
-201.95,
299.95,
-95.95,
25.45,
129.33,
-26.5,
43.5,
122
};
private static final double[] da2 = {
20, 35, 59, 105, 150, -37, -65, -99, -145, -185
};
public final void paintControl( PaintEvent e )
{
Rectangle d = this.getClientArea( );
Image imgChart = new Image( this.getDisplay( ), d );
GC gcImage = new GC( imgChart );
//Look at IDeviceRenderer.java for all properties
//like DPI_RESOLUTION
//FILE_IDENTIFIER
//FORMAT_IDENTIFIER
//UPDATE_NOTIFIER
idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
//idr.setProperty(IDeviceRenderer.UPDATE_NOTIFIER, this);
Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
Generator gr = Generator.instance( );
if ( bNeedsGeneration )
{
bNeedsGeneration = false;
try
{
gcs = gr.build( idr.getDisplayServer( ),
cm,
bo,
null,
null,
null );
}
catch ( ChartException ce )
{
ce.printStackTrace( );
}
}
if ( bFirstPaint )
{
bFirstPaint = false;
}
try
{
gr.render( idr, gcs );
GC gc = e.gc;
gc.drawImage( imgChart, d.x, d.y );
}
catch ( ChartException ex )
{
ex.printStackTrace( );
}
}
}
Jason
On 11/15/2011 6:07 PM, gloeckner.peter wrote:
> Hi,
> This are my two classes.
> The class SwtLiveChartViewer is copied from a tutorial.
> In the class LineChart i am adding the trigger.
>
> bs1.getTriggers().add(TriggerImpl
> create(TriggerCondition.ONMOUSEDOWN_LITERAL, ActionImpl
> create(ActionType.SHOW_TOOLTIP_LITERAL,
> TooltipValueImpl.create(500, null))));
>
> When i'm clicking on a point the tooltipp is displayed. In fact i only
> want to call a function, which i can defined, when the user clicks on a
> point.
>
> Thanks
> Peter
>
>
>
> package org.eclipse.ui.tutorials.rcp.charts;
>
> import org.eclipse.birt.chart.model.Chart;
> import org.eclipse.birt.chart.model.ChartWithAxes;
> import org.eclipse.birt.chart.model.attribute.ActionType;
> import org.eclipse.birt.chart.model.attribute.AxisType;
> import org.eclipse.birt.chart.model.attribute.DataPoint;
> import org.eclipse.birt.chart.model.attribute.DataPointComponentType;
> import org.eclipse.birt.chart.model.attribute.IntersectionType;
> import org.eclipse.birt.chart.model.attribute.LineStyle;
> import org.eclipse.birt.chart.model.attribute.TickStyle;
> import org.eclipse.birt.chart.model.attribute.TriggerCondition;
> import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
> import org.eclipse.birt.chart.model.attribute.impl.DataPointComponentImpl;
> import
> org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl;
> import org.eclipse.birt.chart.model.attribute.impl.TooltipValueImpl;
> import org.eclipse.birt.chart.model.component.Axis;
> import org.eclipse.birt.chart.model.component.Series;
> import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
> import org.eclipse.birt.chart.model.data.NumberDataSet;
> import org.eclipse.birt.chart.model.data.SeriesDefinition;
> import org.eclipse.birt.chart.model.data.impl.ActionImpl;
> import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
> import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
> import org.eclipse.birt.chart.model.data.impl.TriggerImpl;
> import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
> import org.eclipse.birt.chart.model.type.LineSeries;
> import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
> import org.eclipse.swt.widgets.Composite;
>
> public class LineChart extends SwtLiveChartViewer{
>
> public void init(){
> xData = new double[2];
> yData = new double[2];
> xData[0] = 1;
> xData[1] = 2;
> yData[0] = 1;
> yData[1] = 2;
> }
>
> public double[] xData;
> public double[] yData;
>
> /**
> * std - Konstruktor
> * @param parent
> * @param style
> */
> public LineChart(Composite parent, int style) {
> super(parent, style);
> }
>
> /**
> * Chart Eigenschaften festlegen
> */
> public Chart createChart(){
> ChartWithAxes chart = ChartWithAxesImpl.create();
> //x-Achse
> Axis xAxis = chart.getPrimaryBaseAxes()[0];
> xAxis.getTitle().setVisible(false); //Titel
> xAxis.getTitle().getCaption().setValue("x");
> xAxis.getTitle().getCaption().getFont().setBold(false);
> xAxis.getTitle().getCaption().getFont().setSize(11);
> xAxis.getTitle().getCaption().getFont().setName("MS Sans Serif");
> xAxis.getLabel().setVisible(false); //Achsenbeschriftung
> xAxis.getLabel().getCaption().getFont().setSize(20);
> xAxis.getLabel().getCaption().getFont().setName("MS Sans Serif");
> xAxis.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
> xAxis.setType(AxisType.LINEAR_LITERAL);
> xAxis.getOrigin().setType(IntersectionType.VALUE_LITERAL);
> xAxis.getScale().setStep(1.0);
>
> //y-Achse
> Axis yAxis = ((ChartWithAxes) chart).getPrimaryOrthogonalAxis(xAxis);
> yAxis.getTitle().setVisible(false);
> yAxis.getTitle().getCaption().setValue("y");
> yAxis.getTitle().getCaption().getFont().setBold(false);
> yAxis.getTitle().getCaption().getFont().setRotation(90);
> yAxis.getTitle().getCaption().getFont().setSize(11);
> yAxis.getTitle().getCaption().getFont().setName("MS Sans Serif");
> yAxis.getLabel().setVisible(false);
> yAxis.getLabel().getCaption().getFont().setSize(8);
> yAxis.getLabel().getCaption().getFont().setName("MS Sans Serif");
> yAxis.getMajorGrid().getLineAttributes().setVisible(true);
> yAxis.getMajorGrid().getLineAttributes().setColor(ColorDefinitionImpl.GREY());
>
> yAxis.getMajorGrid().getLineAttributes().setStyle(LineStyle.DASHED_LITERAL);
>
> yAxis.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
> yAxis.setType(AxisType.LINEAR_LITERAL);
> yAxis.getOrigin().setType(IntersectionType.VALUE_LITERAL);
> yAxis.getScale().setStep(1.0);
> //TODO
> init();
> //x-Series
> Series xSeries = SeriesImpl.create();
> SeriesDefinition sdX = SeriesDefinitionImpl.create();
> sdX.getSeries().add(xSeries);
> xAxis.getSeriesDefinitions().add(sdX);
> // X-Axis
>
> NumberDataSet dataX = NumberDataSetImpl.create( xData );
> xSeries.setDataSet(dataX);
> NumberDataSet orthoValuesDataSet1 = NumberDataSetImpl.create(yData);
>
> LineSeries bs1 = (LineSeries) LineSeriesImpl.create();
> bs1.setDataSet(orthoValuesDataSet1);
> //bs1.setRiserOutline(null);
>
> DataPoint dp = bs1.getDataPoint();
> dp.getComponents().clear();
> dp.setPrefix("(");
> dp.setSuffix(")");
>
> // Chart doesn't support format specifier for text, so we use
> // JavaNumberFormatSpecifierImpl here.
> dp.getComponents().add(DataPointComponentImpl
> .create(DataPointComponentType.BASE_VALUE_LITERAL,
> JavaNumberFormatSpecifierImpl.create("0")));
> dp.getComponents().add(DataPointComponentImpl
> .create(DataPointComponentType.ORTHOGONAL_VALUE_LITERAL,
> JavaNumberFormatSpecifierImpl.create("0")));
>
> bs1.getTriggers().add(TriggerImpl
> .create(TriggerCondition.ONMOUSEDOWN_LITERAL, ActionImpl
> .create(ActionType.SHOW_TOOLTIP_LITERAL,
> TooltipValueImpl.create(500, null))));
> SeriesDefinition sdY = SeriesDefinitionImpl.create();
> yAxis.getSeriesDefinitions().add(sdY);
> sdY.getSeries().add(bs1);
> return chart;
> }
>
> /**
> * * @param chart
> */
> public void updateDataSet(ChartWithAxes chart)
> {
> Axis xAxis = chart.getPrimaryBaseAxes()[0];
> Axis yAxis = chart.getPrimaryOrthogonalAxis(xAxis);
>
> NumberDataSet dataX = NumberDataSetImpl.create( xData );
> NumberDataSet dataY = NumberDataSetImpl.create( yData );
>
> // X-Axis
> SeriesDefinition sdX = (SeriesDefinition)
> xAxis.getSeriesDefinitions().get(0);
> ((Series) sdX.getSeries().get(0)).setDataSet(dataX);
>
> // Y-Axis
> SeriesDefinition sdY = (SeriesDefinition)
> yAxis.getSeriesDefinitions().get(0);
> ((Series) sdY.getSeries().get(0)).setDataSet(dataY);
> }
>
> public void insertPoint(String x, String y){
> System.out.println("DoubleKLick: " + x + " " + y);
>
> double[] xDataNew = new double[xData.length + 1];
> double[] yDataNew = new double[yData.length + 1];
>
> System.arraycopy(xData, 0, xDataNew, 0, xData.length);
> System.arraycopy(yData, 0, yDataNew, 0, yData.length);
>
> xDataNew[xData.length] = Double.valueOf(x);
> yDataNew[yData.length] = Double.valueOf(y);
>
> xData = xDataNew;
> yData = yDataNew;
>
> updateDataSet((ChartWithAxes) chart);
> }
>
> @Override
> public Chart getDesignTimeModel() {
> return gcs.getChartModel();
> }
>
> @Override
> public Chart getRunTimeModel() {
> return chart;
> }
>
> @Override
> public Object peerInstance() {
> return this;
> }
>
> @Override
> public void regenerateChart() {
> redraw();
> }
>
> @Override
> public void repaintChart() {
> redraw();
> }
> }
>
> //
>
> package org.eclipse.ui.tutorials.rcp.charts;
>
> import org.eclipse.birt.chart.api.ChartEngine;
> import org.eclipse.birt.chart.device.IDeviceRenderer;
> import org.eclipse.birt.chart.device.IUpdateNotifier;
> import org.eclipse.birt.chart.exception.ChartException;
> import org.eclipse.birt.chart.factory.GeneratedChartState;
> import org.eclipse.birt.chart.factory.Generator;
> import org.eclipse.birt.chart.model.Chart;
> import org.eclipse.birt.chart.model.attribute.Bounds;
> import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
> import org.eclipse.birt.core.framework.PlatformConfig;
> import org.eclipse.swt.events.ControlEvent;
> import org.eclipse.swt.events.ControlListener;
> import org.eclipse.swt.events.PaintEvent;
> import org.eclipse.swt.events.PaintListener;
> import org.eclipse.swt.graphics.GC;
> import org.eclipse.swt.graphics.Image;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
>
> public abstract class SwtLiveChartViewer extends Composite implements
> PaintListener, ControlListener, IUpdateNotifier{
>
> private IDeviceRenderer idr = null;
> public Chart chart;
> public GeneratedChartState gcs = null;
>
> /**
> * Used in building the chart for the first time
> */
> private boolean bFirstPaint = true;
>
> /**
> * Constructor
> */
> public SwtLiveChartViewer( Composite parent, int style )
> {
> super( parent, style );
> try
> {
> PlatformConfig config = new PlatformConfig( );
> config.setBIRTHome("");
> idr = ChartEngine.instance( config ).getRenderer( "dv.SWT" );
> idr.setProperty(IDeviceRenderer.UPDATE_NOTIFIER, this);
>
> }
> catch ( ChartException pex )
> {
> pex.printStackTrace();
> }
> chart = createChart( );
> }
>
> @Override
> public void dispose() {
> if( (imgChart !=null) && (!imgChart.isDisposed()))imgChart.dispose();
> if( (gcImage !=null) && (!gcImage.isDisposed()))gcImage.dispose();
> super.dispose();
> }
>
>
>
> public abstract Chart createChart();
>
> private Image imgChart;
> private GC gcImage;
> private Bounds bo;
>
> /*
> * (non-Javadoc)
> * * @see
> * org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events
> * .PaintEvent)
> */
> public final void paintControl( PaintEvent e )
> {
> Rectangle d = this.getClientArea( );
> if ( bFirstPaint )
> {
> //if resized
> if( (imgChart !=null) && (!imgChart.isDisposed()))imgChart.dispose();
> if( (gcImage !=null) && (!gcImage.isDisposed()))gcImage.dispose();
>
> imgChart = new Image( this.getDisplay( ), d );
> gcImage = new GC( imgChart );
> idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
> bo = BoundsImpl.create( 0, 0, d.width, d.height );
> bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
>
> }
> Generator gr = Generator.instance( );
> try
> {
> gcs = gr.build( idr.getDisplayServer( ), chart, bo, null, null, null );
> // gcs.getRunTimeContext().setActionRenderer( new MyActionHandler());
> gr.render( idr, gcs );
> GC gc = e.gc;
> gc.drawImage( imgChart, d.x, d.y );
> }
> catch ( ChartException ce )
> {
> ce.printStackTrace( );
> }
> bFirstPaint = false;
> Display.getDefault( ).timerExec( 500, new Runnable( ) {
> public void run( )
> {
> chartRefresh( );
> }
> } );
>
>
> }
>
> /*
> * (non-Javadoc)
> * * @see
> *
> org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
> * .swt.events.SelectionEvent)
> */
> private void chartRefresh( )
> {
> if ( !isDisposed( ) )
> {
> final Generator gr = Generator.instance( );
> //scrollData( (ChartWithAxes) chart );
> // Refresh
> try
> {
> gr.refresh( gcs );
> }
> catch ( ChartException ex )
> {
> ex.printStackTrace( );
> }
> redraw( );
> }
> }
> public void controlMoved(ControlEvent arg0) {
> bFirstPaint = true;
>
> }
> public void controlResized(ControlEvent arg0) {
> bFirstPaint = true;
> }
> }
>
|
|
|
Re: Drag and Drop function for Bird [message #757840 is a reply to message #757570] |
Mon, 21 November 2011 18:44 |
|
Can you try using the generated chart state object to get the plot
computations like: (Note this only gets the auto scale of the orthogonal
axis) The x-axis will have a discreet number of points as defined in you
dataset. Have a look at the bottom to see how to access these points.
Axis xAxis = ((ChartWithAxes) chart).getPrimaryBaseAxes()[0];
Axis yAxis = xAxis.getAssociatedAxes().get(0);
SeriesDefinition xSerieDef = xAxis.getSeriesDefinitions().get(0);
SeriesDefinition ySerieDef = yAxis.getSeriesDefinitions().get(0);
Series ySerie = ySerieDef.getRunTimeSeries().get(0);
Object min =
gcs.getComputations().getSeriesRenderingHints(ySerieDef,ySerie).getOrthogonalScale().getMaximum(
);
Object max =
gcs.getComputations().getSeriesRenderingHints(ySerieDef,ySerie).getOrthogonalScale().getMinimum(
);
//x and y data points
Object orthodp1 =
gcs.getComputations().getSeriesRenderingHints(ySerieDef,ySerie).getDataPoints()[0].getOrthogonalValue();
catdp1 =
gcs.getComputations().getSeriesRenderingHints(ySerieDef,ySerie).getDataPoints()[0].getBaseValue();
int xaxisdpsize =
gcs.getComputations().getSeriesRenderingHints(ySerieDef,ySerie).getDataPoints().length;
Jason
On 11/19/2011 9:49 AM, gloeckner.peter wrote:
> Hi,
> I could solve some problems. But I have another one :).
> In fact i forgot about triggers and added two listeners to my LineChart.
> The first one is notified when i click on the chart, the second one when
> you release your mouse.
> I found the right methods to compute, where the point is relativly to
> the origin of the graph. (in point not pixel coordinates) Now I need the
> actual MaxValue of the Axis, but the results of these method calls
> (class MouseUpListener, method handleEvent)
>
> Axis xAxis = ((ChartWithAxes) chart).getPrimaryBaseAxes()[0];
> System.out.println(xAxis.getScale().getMin());
> System.out.println(xAxis.getScale().getMax());
>
> are null.
>
> When I'm assigning a max and min value, like this
> // xAxis.getScale().setMin(NumberDataElementImpl.create(0));
> // xAxis.getScale().setMax(NumberDataElementImpl.create(3));
> I will loose the autoExpand ability.
>
> Below you can see my two classes, my LineChart can be instantiated in
> your RCP view like this:
>
> c3dViewer = new LineChart( all, SWT.NO_BACKGROUND );
> c3dViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) );
> c3dViewer.addPaintListener( c3dViewer );
> c3dViewer.addControlListener( c3dViewer );
> c3dViewer.setDragAndDropEnabled(true);
>
> //
>
> package org.eclipse.ui.tutorials.rcp.charts;
>
>
>
> import org.eclipse.birt.chart.api.ChartEngine;
>
> import org.eclipse.birt.chart.device.IDeviceRenderer;
>
> import org.eclipse.birt.chart.device.IUpdateNotifier;
>
> import org.eclipse.birt.chart.exception.ChartException;
>
> import org.eclipse.birt.chart.factory.GeneratedChartState;
>
> import org.eclipse.birt.chart.factory.Generator;
>
> import org.eclipse.birt.chart.model.Chart;
>
> import org.eclipse.birt.chart.model.attribute.Bounds;
>
> import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
>
> import org.eclipse.birt.core.framework.PlatformConfig;
>
> import org.eclipse.swt.events.ControlEvent;
>
> import org.eclipse.swt.events.ControlListener;
>
> import org.eclipse.swt.events.PaintEvent;
>
> import org.eclipse.swt.events.PaintListener;
>
> import org.eclipse.swt.graphics.GC;
>
> import org.eclipse.swt.graphics.Image;
>
> import org.eclipse.swt.graphics.Rectangle;
>
> import org.eclipse.swt.widgets.Composite;
>
> import org.eclipse.swt.widgets.Display;
>
>
>
> public abstract class SwtLiveChartViewer extends Composite implements
> PaintListener, ControlListener, IUpdateNotifier{
>
>
>
> private IDeviceRenderer idr = null;
>
> public Chart chart;
>
> public GeneratedChartState gcs = null;
>
>
>
> /**
>
> * Used in building the chart for the first time
>
> */
>
> private boolean bFirstPaint = true;
>
>
>
> /**
>
> * Constructor
>
> */
>
> public SwtLiveChartViewer( Composite parent, int style )
>
> {
>
> super( parent, style );
>
> try
>
> {
>
> PlatformConfig config = new PlatformConfig( );
>
> config.setBIRTHome("");
>
> idr = ChartEngine.instance( config ).getRenderer( "dv.SWT" );
>
> idr.setProperty(IDeviceRenderer.UPDATE_NOTIFIER, this);
>
>
>
> }
>
> catch ( ChartException pex )
>
> {
>
> pex.printStackTrace();
>
> }
>
> chart = createChart( );
>
> }
>
>
>
> @Override
>
> public void dispose() {
>
> if( (imgChart !=null) && (!imgChart.isDisposed()))imgChart.dispose();
>
> if( (gcImage !=null) && (!gcImage.isDisposed()))gcImage.dispose();
>
> super.dispose();
>
> }
>
>
>
>
>
>
>
> public abstract Chart createChart();
>
>
>
> private Image imgChart;
>
> private GC gcImage;
>
> private Bounds bo;
>
>
>
> /*
>
> * (non-Javadoc)
>
> *
> * @see
>
> * org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events
>
> * .PaintEvent)
>
> */
>
> public final void paintControl( PaintEvent e )
>
> {
>
> Rectangle d = this.getClientArea( );
>
>
>
> if ( bFirstPaint )
>
> {
>
> //if resized
>
> if( (imgChart !=null) && (!imgChart.isDisposed()))imgChart.dispose();
>
> if( (gcImage !=null) && (!gcImage.isDisposed()))gcImage.dispose();
>
>
>
> imgChart = new Image( this.getDisplay( ), d );
>
> gcImage = new GC( imgChart );
>
>
>
> idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
>
> bo = BoundsImpl.create( 0, 0, d.width, d.height );
>
>
>
>
>
>
>
>
>
>
>
>
>
> bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
>
>
>
> }
>
> Generator gr = Generator.instance( );
>
> try
>
> {
>
> gcs = gr.build( idr.getDisplayServer( ), chart, bo, null, null, null );
>
> // gcs.getRunTimeContext().setActionRenderer( new MyActionHandler());
>
>
>
> gr.render( idr, gcs );
>
> GC gc = e.gc;
>
> gc.drawImage( imgChart, d.x, d.y );
>
> }
>
> catch ( ChartException ce )
>
> {
>
> ce.printStackTrace( );
>
> }
>
> bFirstPaint = false;
>
> Display.getDefault( ).timerExec( 500, new Runnable( ) {
>
> public void run( )
>
> {
>
> chartRefresh( );
>
> }
>
> } );
>
>
>
>
>
> }
>
>
>
> /*
>
> * (non-Javadoc)
>
> *
> * @see
>
> *
> org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
>
> * .swt.events.SelectionEvent)
>
> */
>
> private void chartRefresh( )
>
> {
>
> if ( !isDisposed( ) )
>
> {
>
> final Generator gr = Generator.instance( );
>
> //scrollData( (ChartWithAxes) chart );
>
> // Refresh
>
> try
>
> {
>
> gr.refresh( gcs );
>
> }
>
> catch ( ChartException ex )
>
> {
>
> ex.printStackTrace( );
>
> }
>
> redraw( );
>
> }
>
> }
>
> public void controlMoved(ControlEvent arg0) {
>
> bFirstPaint = true;
>
>
>
> }
>
> public void controlResized(ControlEvent arg0) {
>
> bFirstPaint = true;
>
> }
>
> }
>
> package org.eclipse.ui.tutorials.rcp.charts;
>
> import org.eclipse.birt.chart.model.Chart;
> import org.eclipse.birt.chart.model.ChartWithAxes;
> import org.eclipse.birt.chart.model.attribute.AxisType;
> import org.eclipse.birt.chart.model.attribute.Bounds;
> import org.eclipse.birt.chart.model.attribute.DataPoint;
> import org.eclipse.birt.chart.model.attribute.DataPointComponentType;
> import org.eclipse.birt.chart.model.attribute.IntersectionType;
> import org.eclipse.birt.chart.model.attribute.LineStyle;
> import org.eclipse.birt.chart.model.attribute.TickStyle;
> import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
> import org.eclipse.birt.chart.model.attribute.impl.DataPointComponentImpl;
> import
> org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl;
> import org.eclipse.birt.chart.model.component.Axis;
> import org.eclipse.birt.chart.model.component.Series;
> import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
> import org.eclipse.birt.chart.model.data.NumberDataSet;
> import org.eclipse.birt.chart.model.data.SeriesDefinition;
> import org.eclipse.birt.chart.model.data.impl.NumberDataElementImpl;
> import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
> import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
> import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
> import org.eclipse.birt.chart.model.type.LineSeries;
> import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Listener;
>
> public class LineChart extends SwtLiveChartViewer{
>
> private boolean dragAndDropEnabled = false;
> private MouseDownListener MouseDownListener;
> private MouseUpListener MouseUpListener;
>
> public boolean isDragAndDropEnabled() {
> return dragAndDropEnabled;
> }
>
> public void setDragAndDropEnabled(boolean dragAndDropEnabled) {
> this.dragAndDropEnabled = dragAndDropEnabled;
> if(this.dragAndDropEnabled){
> this.MouseUpListener = new MouseUpListener();
> this.MouseDownListener = new MouseDownListener(this.MouseUpListener);
> addListener(SWT.MouseDown, this.MouseDownListener);
> addListener(SWT.MouseUp, this.MouseUpListener);
> }else{
> removeListener(SWT.MouseDown, this.MouseDownListener);
> removeListener(SWT.MouseUp, this.MouseUpListener);
> }
> }
>
> public void init(){
> xData = new double[2];
> yData = new double[2];
> xData[0] = 1;
> xData[1] = 2;
> yData[0] = 1;
> yData[1] = 2;
> }
>
> public double[] xData;
> public double[] yData;
>
> /**
> * std - Konstruktor
> * @param parent
> * @param style
> */
> public LineChart(Composite parent, int style) {
> super(parent, style);
> }
>
> /**
> * Chart Eigenschaften festlegen
> */
> public Chart createChart(){
> ChartWithAxes chart = ChartWithAxesImpl.create();
> //x-Achse
> Axis xAxis = chart.getPrimaryBaseAxes()[0];
> xAxis.getTitle().setVisible(true); //Titel
> xAxis.getTitle().getCaption().setValue("x");
> xAxis.getTitle().getCaption().getFont().setBold(false);
> xAxis.getTitle().getCaption().getFont().setSize(11);
> xAxis.getTitle().getCaption().getFont().setName("MS Sans Serif");
> xAxis.getLabel().setVisible(true); //Achsenbeschriftung
> xAxis.getLabel().getCaption().getFont().setSize(11);
> xAxis.getLabel().getCaption().getFont().setName("MS Sans Serif");
> xAxis.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
> xAxis.setType(AxisType.LINEAR_LITERAL);
> xAxis.getOrigin().setType(IntersectionType.VALUE_LITERAL);
> xAxis.getScale().setStep(1.0);
> // xAxis.getScale().setMin(NumberDataElementImpl.create(0));
> // xAxis.getScale().setMax(NumberDataElementImpl.create(3));
> xAxis.getScale().setAutoExpand(true);
>
> //y-Achse
> Axis yAxis = ((ChartWithAxes) chart).getPrimaryOrthogonalAxis(xAxis);
> yAxis.getTitle().setVisible(true);
> yAxis.getTitle().getCaption().setValue("y");
> yAxis.getTitle().getCaption().getFont().setBold(false);
> yAxis.getTitle().getCaption().getFont().setRotation(90);
> yAxis.getTitle().getCaption().getFont().setSize(11);
> yAxis.getTitle().getCaption().getFont().setName("MS Sans Serif");
> yAxis.getLabel().setVisible(true);
> yAxis.getLabel().getCaption().getFont().setSize(8);
> yAxis.getLabel().getCaption().getFont().setName("MS Sans Serif");
> yAxis.getMajorGrid().getLineAttributes().setVisible(true);
> yAxis.getMajorGrid().getLineAttributes().setColor(ColorDefinitionImpl.GREY());
>
> yAxis.getMajorGrid().getLineAttributes().setStyle(LineStyle.DASHED_LITERAL);
>
> yAxis.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
> yAxis.setType(AxisType.LINEAR_LITERAL);
> yAxis.getOrigin().setType(IntersectionType.VALUE_LITERAL);
> yAxis.getScale().setStep(1.0);
> //TODO
> init();
> //x-Series
> Series xSeries = SeriesImpl.create();
> SeriesDefinition sdX = SeriesDefinitionImpl.create();
> sdX.getSeries().add(xSeries);
> xAxis.getSeriesDefinitions().add(sdX);
> // X-Axis
>
> NumberDataSet dataX = NumberDataSetImpl.create( xData );
> xSeries.setDataSet(dataX);
> NumberDataSet orthoValuesDataSet1 = NumberDataSetImpl.create(yData);
>
> LineSeries bs1 = (LineSeries) LineSeriesImpl.create();
> bs1.setDataSet(orthoValuesDataSet1);
> //bs1.setRiserOutline(null);
>
> DataPoint dp = bs1.getDataPoint();
> dp.getComponents().clear();
> dp.setPrefix("(");
> dp.setSuffix(")");
>
> // Chart doesn't support format specifier for text, so we use
> // JavaNumberFormatSpecifierImpl here.
> dp.getComponents().add(DataPointComponentImpl
> .create(DataPointComponentType.BASE_VALUE_LITERAL,
> JavaNumberFormatSpecifierImpl.create("0")));
> dp.getComponents().add(DataPointComponentImpl
> .create(DataPointComponentType.ORTHOGONAL_VALUE_LITERAL,
> JavaNumberFormatSpecifierImpl.create("0")));
>
> /* bs1.getTriggers().add(TriggerImpl
> .create(TriggerCondition.ONMOUSEDOWN_LITERAL, ActionImpl
> .create(ActionType.SHOW_TOOLTIP_LITERAL,
> TooltipValueImpl.create(500, null))));*/
> SeriesDefinition sdY = SeriesDefinitionImpl.create();
> yAxis.getSeriesDefinitions().add(sdY);
> sdY.getSeries().add(bs1);
> //Farben
> chart.getPlot().getClientArea().setBackground(ColorDefinitionImpl.BLUE());
> chart.getPlot().setBackground(ColorDefinitionImpl.GREEN());
> chart.getLegend().setBackground(ColorDefinitionImpl.CYAN());
> chart.getTitle().setBackground(ColorDefinitionImpl.ORANGE());
> chart.getPlot().getOutline().setColor(ColorDefinitionImpl.BLACK());
> return chart;
> }
>
> /**
> * * @param chart
> */
> public void updateDataSet(ChartWithAxes chart)
> {
> Axis xAxis = chart.getPrimaryBaseAxes()[0];
> Axis yAxis = chart.getPrimaryOrthogonalAxis(xAxis);
>
> NumberDataSet dataX = NumberDataSetImpl.create( xData );
> NumberDataSet dataY = NumberDataSetImpl.create( yData );
>
> // X-Axis
> SeriesDefinition sdX = (SeriesDefinition)
> xAxis.getSeriesDefinitions().get(0);
> ((Series) sdX.getSeries().get(0)).setDataSet(dataX);
>
> // Y-Axis
> SeriesDefinition sdY = (SeriesDefinition)
> yAxis.getSeriesDefinitions().get(0);
> ((Series) sdY.getSeries().get(0)).setDataSet(dataY);
> }
>
> public void insertPoint(String x, String y){
> System.out.println("DoubleKLick: " + x + " " + y);
>
> double[] xDataNew = new double[xData.length + 1];
> double[] yDataNew = new double[yData.length + 1];
>
> System.arraycopy(xData, 0, xDataNew, 0, xData.length);
> System.arraycopy(yData, 0, yDataNew, 0, yData.length);
>
> xDataNew[xData.length] = Double.valueOf(x);
> yDataNew[yData.length] = Double.valueOf(y);
>
> xData = xDataNew;
> yData = yDataNew;
>
> updateDataSet((ChartWithAxes) chart);
> }
>
>
>
> @Override
> public Chart getDesignTimeModel() {
> return gcs.getChartModel();
> }
>
> @Override
> public Chart getRunTimeModel() {
> return chart;
> }
>
> @Override
> public Object peerInstance() {
> return this;
> }
>
> @Override
> public void regenerateChart() {
> redraw();
> }
>
> @Override
> public void repaintChart() {
> redraw();
> }
>
> public class MouseDownListener implements Listener{
>
> private MouseUpListener mouseUpListener;
>
> public MouseDownListener(MouseUpListener mouseUpListener){
> this.mouseUpListener = mouseUpListener;
> }
>
> @Override
> public void handleEvent(Event event) {
> mouseUpListener.setStart(event);
> }
>
> }
>
> public class MouseUpListener implements Listener{
>
> private Event start = null;
>
> public void setStart(Event start){
> this.start = start;
> }
>
> @Override
> public void handleEvent(Event event) {
>
> //
> Bounds clientarea = gcs.getComputations().getPlotBounds();
> double start_y = clientarea.getTop() + clientarea.getHeight() - start.y;
> double start_x = start.x - clientarea.getLeft();
>
> System.out.println(start_x + " , " + start_y);
>
> //
> Axis xAxis = ((ChartWithAxes) chart).getPrimaryBaseAxes()[0];
> System.out.println(xAxis.getScale().getMin());
> System.out.println(xAxis.getScale().getMax());
>
> }
>
> }
> }
>
>
>
>
|
|
| |
Goto Forum:
Current Time: Tue Apr 23 10:34:56 GMT 2024
Powered by FUDForum. Page generated in 0.04377 seconds
|