import org.eclipse.rap.addons.chart.NvChart;
import org.eclipse.rap.addons.chart.internal.ColorUtil;
import org.eclipse.rap.json.JsonArray;
import org.eclipse.rap.json.JsonObject;
import org.eclipse.rap.json.JsonValue;
import org.eclipse.swt.widgets.Composite;
public class BulletChart extends NvChart {
public BulletChart(Composite parent, int style) {
super(parent, style, "nv-bullet");
requireJs(registerResource("chart/nv/nv-bullet.js"));
}
public void setItems(BulletChartDataItem... items) {
JsonArray values = new JsonArray();
for (BulletChartDataItem item : items) {
values.add(toJson(item));
}
System.out.println( values.toString() );
setItems(values);
}
public static JsonObject toJson(BulletChartDataItem item) {
JsonObject json = new JsonObject().add("title", item.title);
if (item.subTitle != null) {
json.add("subTitle", item.subTitle);
}
if (item.ranges != null) {
JsonArray arr = new JsonArray();
for (double d : item.ranges) {
arr.add(d);
}
json.add("ranges", arr);
}
if (item.measures != null) {
JsonArray arr = new JsonArray();
for (double d : item.measures) {
arr.add(d);
}
json.add("measures", arr);
}
if (item.markers != null) {
JsonArray arr = new JsonArray();
for (double d : item.markers) {
arr.add(d);
}
json.add("markers", arr);
}
if (item.color != null) {
json.add("color", ColorUtil.toHtmlString(item.color));
}
return json;
}
}
import org.eclipse.swt.graphics.RGB;
public class BulletChartDataItem {
protected final String title;
protected final String subTitle;
protected final double[] ranges;
protected final double[] measures;
protected final double[] markers;
protected final RGB color;
public BulletChartDataItem(String text, double[] ranges, double[] measures, double[] markers) {
this(text, null, ranges, measures, markers, null);
}
public BulletChartDataItem(String text, String subTitle, double[] ranges, double[] measures, double[] markers) {
this(text, subTitle, ranges, measures, markers, null);
}
public BulletChartDataItem(String text, String subTitle, double[] ranges, double[] measures, double[] markers,
RGB color) {
super();
this.title = text;
this.subTitle = subTitle;
this.ranges = ranges;
this.measures = measures;
this.markers = markers;
this.color = color;
}
public String getTitle() {
return title;
}
public String getSubTitle() {
return subTitle;
}
public double[] getRanges() {
return ranges;
}
public double[] getMeasures() {
return measures;
}
public double[] getMarkers() {
return markers;
}
public RGB getColor() {
return color;
}
}
import org.eclipse.rap.addons.chart.basic.BulletChart;
import org.eclipse.rap.addons.chart.basic.BulletChartDataItem;
import org.eclipse.rap.rwt.application.AbstractEntryPoint;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
public class BulletChartSnippet extends AbstractEntryPoint {
private BulletChart bulletChart;
@Override
public void createContents( Composite parent ) {
parent.setLayout( new GridLayout() );
createBulletChart( parent );
createUpdateButton( parent );
update();
}
private void createBulletChart( Composite parent ) {
bulletChart = new BulletChart( parent, SWT.NONE );
bulletChart.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
bulletChart.addListener( SWT.Selection, new Listener() {
@Override
public void handleEvent( Event event ) {
System.out.println( "Selected bullet item #" + event.index );
}
} );
}
private void createUpdateButton( Composite parent ) {
Button button = new Button( parent, SWT.PUSH );
button.setText( "Change data" );
button.addListener( SWT.Selection, new Listener() {
@Override
public void handleEvent( Event event ) {
update();
}
} );
}
private void update() {
bulletChart.setItems( createItems() );
}
private static BulletChartDataItem[] createItems() {
return new BulletChartDataItem[] {
new BulletChartDataItem( "Revenue", "US$, in thousands", new double[] { 150, 225, 300 }, new double[] { 220, 270 }, new double[] { 250 } ),
new BulletChartDataItem( "Profit", "%", new double[] { 20, 25, 30 }, new double[] { 21, 23 }, new double[] { 26 } ),
new BulletChartDataItem( "Order Size", "US$, average", new double[] { 350, 500, 600 }, new double[] { 100, 320 }, new double[] { 550 } ),
new BulletChartDataItem( "New Customers", "count", new double[] { 1400, 2000, 2500 }, new double[] { 1000, 1650 }, new double[] { 2100 } ),
new BulletChartDataItem( "Satisfaction", "out of 5", new double[] { 3.5, 4.25, 5}, new double[] { 3.2, 4.7}, new double[] { 4.4 } ),
};
}
}
]]>