How to save/restore the views in eclipse e4 - java

I have two sections. In the left section I am using checkbox tableviewer for displaying list of file names. The right section is for showing graphs(I am using JFreechart). I have a handler which is used for dynamically adding tabs to the right side section. IF I am in first tab and made some checkbox selections in left side tableviewer ,the graph is displayed in right side. When I create a new tab(right side) , the left side tableviewer should reset.
When I select the first tab again and I want to see the previous selection in left side section.Can anyone please give some ideas how to save/restore the views based on the tab change?
Left side section code for file viewer:
#PostConstruct
public void createComposite(Composite parent) {
parent.setLayout(new GridLayout(1, false));
tableViewer = new CheckboxTableViewer(parent, SWT.BORDER);
tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
}
public void setTableInput(File[] selectedFiles) {
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
tableViewer.setLabelProvider(new FileLabelProvider());
prevSelectedFiles = selectedFiles;
tableViewer.setInput(selectedFiles);
tableViewer.addCheckStateListener(new ICheckStateListener() {
#Override
public void checkStateChanged(CheckStateChangedEvent event) {
filesSelected = tableViewer.getCheckedElements();
// some code to display graph
}}}
Right side code(Graph)
#PostConstruct
public void postConstruct(final Composite parent) {
final JFreeChart chart = createChart(dataset, title);
new ChartComposite(parent, SWT.NONE, chart, true);
}
private JFreeChart createChart(TimeSeriesCollection dataset, String string) {
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"REPORT GENERATION", "TimeStamp", "ms", dataset, true, true,
false);
chart.setBackgroundPaint(Color.WHITE);
final XYPlot plot = (XYPlot) chart.getPlot();
plot.setDataset(0, dataset);
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinePaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.BLACK);
Shape shape = new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
.getRenderer();
renderer.setSeriesShape(0, shape);
renderer.setSeriesShape(1, shape);
renderer.setSeriesShape(2, shape);
renderer.setBaseShapesVisible(true);
renderer.setSeriesOutlinePaint(0, Color.GRAY);
renderer.setSeriesOutlinePaint(1, Color.GRAY);
renderer.setSeriesOutlinePaint(2, Color.GRAY);
renderer.setUseFillPaint(true);
renderer.setSeriesFillPaint(0, Color.red);
renderer.setSeriesFillPaint(1, Color.green);
renderer.setSeriesFillPaint(2, Color.blue);
renderer.setSeriesPaint(0, Color.red);
renderer.setSeriesPaint(1, Color.green);
renderer.setSeriesPaint(2, Color.blue);
NumberAxis yaxis = (NumberAxis) plot.getRangeAxis();
yaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
yaxis.setAutoRangeIncludesZero(false);
plot.setRangeAxis(yaxis);
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setAutoTickUnitSelection(true);
// axis.setTickUnit(new DateTickUnit(DateTickUnitType.SECOND,120));
axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));
// DateAxis.createStandardDateTickUnits();
axis.setTickMarksVisible(true);
axis.setTickLabelsVisible(true);
return chart;
}
public void setValue(ArrayList<TreeMap<Timestamp, Long>> statisticalValues, String protocolName, String statistics) {
// //System.out
// .println("setting the value for timeseries-->" + i);
TimeSeries ts = null;
for (TreeMap<Timestamp, Long> entries : statisticalValues) {
ts = new TimeSeries(protocolName + "_" + statistics,
Second.class);
for (Entry<Timestamp, Long> seriesData : entries.entrySet()) {
ts.addOrUpdate(new Second(seriesData.getKey()),
seriesData.getValue());
}
}
dataset.addSeries(ts);
}
The handler responsible for dynamic creation of tab(Grapgh part):
public class DynamicPartsHandler {
#Execute
public void execute(EPartService partService, EModelService modelService,
MApplication application,Shell shell) {
String partName = "Graph";
MPart part = partService
.createPart("com.wincor.commtrace.project.partDescriptor.1");
MPartStack stack = (MPartStack) modelService.find(
"com.wincor.commtrace.project.partstack.2", application);
stack.getChildren().add(part);
part.setLabel(partName);
part.setVisible(true);
part.setCloseable(true);
partService.showPart(part, PartState.ACTIVATE);
}
}
Thanks in advance

Related

JFreeChart: custom chart with tooltip on mouseover

I have requirement to show a XyLineChart with adding data dynamically. I have used the chart Customizer to read data from db with some additional logic and adding that to chart. But I am not able to create tool tip on mouse over for each data points on chart. following is my code for Customizer.
What is the correct way to create Tool tip on Mouse over?
public class MyChartCustomizer extends JRAbstractChartCustomizer{
#Override
public void customize(JFreeChart chart, JRChart jrChart) {
XyPlot plot= chart.getXyPlot;
XYSeriesCollection ds = (XYSeriesCollection) plot.getDataset();
XYSeries x1 = new XYSeries("C 1", true, true);
x1.add(10,20);
XYBarRenderer ren = (XYBarRenderer) plot.getRenderer();
plot.setRenderer(ren);
ren.setSeriesToolTipGenerator(0, new XYToolTipGenerator() {
#Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return "C 1";
}
});
ren.setToolTipGenerator(new XYToolTipGenerator() {
#Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return "C 1";
}
});
chart.fireChartChanged();
}
}
}
Thank you, Petter and Trashgod helping me out to find the solution for this issue.
The actual issue is, if we add the new data using customizer then the new data point get display on the graph but respected tool tip does not get generated and the map used for tooltip will not be updated on html code. Since I have to use jasper server as per requirement I implemented following work around other way will be simply generate chart image with map using jfreechart API and display on the jsp page(no need for report design....)..
Following is the way I Implemented this.
I used the following code to generate the same chart which jasperserver/jasper studio create the chart. this gives me same chart which internally get created and I create the map for tooltips and passing it as parameter to browser and using javascript function inserting the new map html code with the chart image.
XYSeriesCollection xyDataSet = new XYSeriesCollection();
JFreeChart chart = ChartFactory.createXYLineChart(
cur_chart.getTitle(),
cur_chart.getxLabel(), cur_chart.getyLabel(),
xyDataSet,
PlotOrientation.VERTICAL,
true,
true,
false);
String chartId = null;
for ( Object tt : chart.getSubtitles()){
if (tt instanceof TextTitle){
chartId= ((TextTitle) tt).getText();
}
}
XYPlot plot = chart.getXYPlot();
//following code to set font size and color is required so that same chart with matching tooltip pixels can we generated.
LegendItemCollection legends = plot.getLegendItems();
List<JRSeriesColor> colors = new ArrayList<JRSeriesColor>();
System.out.println("Customizer: "+ chartId);
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
Color trans = new Color(0xFF, 0xFF, 0xFF, 0);
chart.setBackgroundPaint(trans);
plot .setBackgroundPaint(trans);
chart.getLegend().setBackgroundPaint(trans);
chart.setTitle(cur_chart.getTitle());
Font font3 = new Font("Dialog", Font.PLAIN, 10);
plot.getDomainAxis().setLabelFont(font3);
plot.getRangeAxis().setLabelFont(font3);
plot.getDomainAxis().setLabelPaint(Color.BLACK);
plot.getRangeAxis().setLabelPaint(Color.BLACK);
//some more code to add real time data to XyDataset,
ToolTipTagFragmentGenerator tooltipConstructor = new ToolTipTagFragmentGenerator() {
public String generateToolTipFragment(String arg0) {
String toolTip = " title = \"" + arg0.replace("\"", "") + "\"";
return (toolTip);
}
};
URLTagFragmentGenerator urlConstructor = new URLTagFragmentGenerator() {
public String generateURLFragment(String arg0) {
String address = " href=\"ControllerAddress\\methodName?"
+ arg0.replace("\"", "") + "\"";
return (address);
}
};
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
// BufferedImage bi chart.createBufferedImage(272, 178, info);
TextTitle tt = new TextTitle("chart1");
tt.setFont(font3);
chart.addSubtitle(tt);
ChartUtilities.saveChartAsPNG(new File("/tmp/test.png"), chart, 500, 250, info);
String map = ChartUtilities.getImageMap(cur_chart.getName(), info, tooltipConstructor, urlConstructor);

Remove BarDataSet in MPAndroidChart

I'm using MPAndroidChart to build BarChart, but I can't set my BarDataSet(1) and set my column's content(2) to vertical. Can anyone help?
public void LoadChart(final ArrayList<String> nameDate, ArrayList<Float> fl){
com.github.mikephil.charting.charts.BarChart chart = (com.github.mikephil.charting.charts.BarChart) findViewById(R.id.barchart);
/* final String[] nameDate={"JAN","FEB","MAR","APR","MAY","JUN"};
Float[] fl = new Float[]{30f,80f,60f,50f,70f,60f};*/
List<BarEntry> entries = new ArrayList<>();
for(int i=0;i<fl.size();i++)
{
entries.add(new BarEntry(i, fl.get(i)));
}
mDB.open();
BarDataSet set = new BarDataSet(entries, "BarDataSet");
set.setColor(Color.rgb(103,203,27));
BarData data = new BarData(set);
data.setBarWidth(0.9f); // set custom bar width
chart.setData(data);
chart.setFitBars(true); // make the x-axis fit exactly all bars
chart.animateXY(2000, 2000);
chart.setScaleEnabled(false);
chart.invalidate(); // refresh
Description description = chart.getDescription();
description.setEnabled(false);
XAxis xAxis = chart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return nameDate.get((int) value);
}
});
YAxis yLabels = chart.getAxisLeft();
yLabels.setDrawGridLines(false);
YAxis yLabels1 = chart.getAxisRight();
yLabels1.setEnabled(false);
}
Yes, is possible, just using following code:
mChart.setDescription(""); // Hide the description
mChart.getAxisLeft().setDrawLabels(false);
mChart.getAxisRight().setDrawLabels(false);
mChart.getXAxis().setDrawLabels(false);
mChart.getLegend().setEnabled(false);
try this
OR
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setExtraOffsets(0, 0, 0,0 );
pieChart.setDragDecelerationFrictionCoef(0.95f);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterText(generateCenterSpannableText());
pieChart.setDrawHoleEnabled(false);
// mChart.setHoleColor(Color.WHITE);
// mChart.setTransparentCircleColor(Color.WHITE);
// mChart.setTransparentCircleAlpha(110);
// mChart.setHoleRadius(58f);
// mChart.setTransparentCircleRadius(61f);
pieChart.setDrawCenterText(false);
pieChart.setRotationAngle(0);
// enable rotation of the chart by touch
pieChart.setRotationEnabled(false);
pieChart.setHighlightPerTapEnabled(false);
// mChart.setUnit(" €");
// mChart.setDrawUnitsInChart(true);
// add a selection listener
pieChart.setOnChartValueSelectedListener(this);
setData(4, 100);
pieChart.animateY(0, Easing.EasingOption.EaseInOutQuad);
// mChart.spin(2000, 0, 360);
// mSeekBarX.setOnSeekBarChangeListener(this);
// mSeekBarY.setOnSeekBarChangeListener(this);
Legend l = pieChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
// mChart.setDescription(""); // Hide the description
// mChart.getAxisLeft().setDrawLabels(false);
// mChart.getAxisRight().setDrawLabels(false);
// mChart.getXAxis().setDrawLabels(false);
pieChart.getLegend().setEnabled(false); // Hi
// entry label styling
pieChart.setEntryLabelColor(Color.WHITE);
// mChart.setEntryLabelTypeface(mTfRegular);
pieChart.setEntryLabelTextSize(12f);
return rootView;
and try this code bar data set is not visible again

cannot access setDomainAxis() method in my JFreeChart plot

I have generated a few charts, as per an assignment, and for data analysis I would like the domain axis to scale to my specifications, rather than to automatically fit the data in each series.
I found a solution here, which seemed very simple to follow: JFReeChart x axis scale
However, after I create my plot, with the code below, it seems like the method does not exist.
Is the method setDomainAxis() one that is included in the libraries or do I have to create it myself? Or have I violated some fundamental rule of java that has nothing to do with JFreeChart? I am a beginner java programmer, so please inform your answer accordingly. Thanks!
Here is my XYPlot class:
public class XYPlot extends JFrame {
private String title;
private String xAxis;
private String yAxis;
private XYSeriesCollection dataset;
public XYPlot(String header, String graphTitle, String xax, String yax) {
super(header);
xAxis = xax;
yAxis = yax;
title = graphTitle;
XYPlot myPlot;
// dataset = dat;
}
public void setTit(String newTitle){
title = newTitle;
}
public void setXAxis(String X){
xAxis = X;
}
public void setYAxis(String Y){
xAxis = Y;
}
public void passData(XYSeriesCollection data){
dataset = data;
}
public void createChart(XYSeriesCollection dataIn){
final JFreeChart chart = ChartFactory.createXYLineChart(
title,
xAxis,
yAxis,
dataIn
);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(1200, 800));
setContentPane(chartPanel);
}
And here is the implementation of the plot in my main program:
public static void seriesPlotter(XYSeriesCollection dataset, String title, String header, String xAxis, String yAxis) {
XYPlot myPlot = new XYPlot(header, title, xAxis, yAxis);
myPlot.createChart(dataset);
NumberAxis domain = new NumberAxis();
domain.setTickUnit(1);
myPlot.setDomainAxis(domain);
myPlot.pack();
myPlot.setVisible(true);
}
Your fragment's use of XYPlot is inconsistent with the API. In particular, there is no such constructor, and there is no method named createChart(). Verify that you are using the current version, 1.0.19, available here.
The methods of ChartFactory are an excellent guide to creating a chart from individual subcomponents. As a concrete example, ChartFactory.createXYLineChart() is recapitulated here. Note how the axes are passed to the XYPlot constructor, obviating the need to invoke setDomainAxis() explicitly. In outline,
// axes
NumberAxis domain = new NumberAxis(xAxis);
NumberAxis range = new NumberAxis(yAxis);
// renderer
XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
// plot
XYPlot plot = new XYPlot(dataset, domain, range, renderer);
// chart
JFreeChart chart = new JFreeChart(
title", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
Maybe you could take a second look after I edit to add the additional code.
Your XYPlot is a JFrame which has no setDomainAxis() method; rename your class to avoid the conflict, e.g. MyXYPlot.
Let your renamed class have a member variable to hold a reference to the org.jfree.chart.plot.XYPlot and add a method to update the name.
XYPlot myPlot;
…
public void setDomainAxisName(String name){
myPlot.getDomainAxis().setLabel(name);
}
There's no reason to extend JFrame.
My technical vocabulary is nil.
Experimenting with JFreeChart is an excellent opportunity to learn.

JFreeChart Bar chart Design Change

I am looking for a way to change the design of the Bar chart in JFreechart. Here is my excel graph. How can I change the jfreechart barchart in the below design format?
The following code can generate a similar chart. Does it suffice?
public class BarExample {
public static void main(String arg[]) throws IOException {
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.setValue(2, Integer.valueOf(1), Integer.valueOf(5));
dataSet.setValue(7, Integer.valueOf(1), Integer.valueOf(10));
dataSet.setValue(4, Integer.valueOf(1), Integer.valueOf(15));
dataSet.setValue(9, Integer.valueOf(1), Integer.valueOf(20));
dataSet.setValue(6, Integer.valueOf(1), Integer.valueOf(25));
JFreeChart chart = ChartFactory.createBarChart
("Chart", "Number of Days", "Number of ECR", dataSet,
PlotOrientation.VERTICAL, false, true, false);
CategoryPlot plot = chart.getCategoryPlot();
// Reduce margin between bars
plot.getDomainAxis().setCategoryMargin(0.0);
// Reduce left and right margin
plot.getDomainAxis().setLowerMargin(0.0);
plot.getDomainAxis().setUpperMargin(0.0);
FileOutputStream outputStream = new FileOutputStream(
new File("chart.png"));
ChartUtilities.writeChartAsPNG(outputStream, chart, 1024, 768);
}
}

Save/Load jFreechart TimeSeriesCollection chart from XML

I'm working with this exemple wich put rondom dynamic data into a TimeSeriesCollection chart.
My problem is that i can't find how to :
1- Make a track of the old data (of the last hour) when they pass the left boundary (because the data points move from the right to the left ) of the view area just by implementing a horizontal scroll bar.
2- Is XML a good choice to save my data into when i want to have all the history of the data?
public class DynamicDataDemo extends ApplicationFrame {
/** The time series data. */
private TimeSeries series;
/** The most recent value added. */
private double lastValue = 100.0;
public DynamicDataDemo(final String title) {
super(title);
this.series = new TimeSeries("Random Data", Millisecond.class);
final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
final JPanel content = new JPanel(new BorderLayout());
content.add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(content);
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
"Dynamic Data Demo",
"Time",
"Value",
dataset,
true,
true,
false
);
final XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000.0); // 60 seconds
axis = plot.getRangeAxis();
axis.setRange(0.0, 200.0);
return result;
}
public void go() {
final double factor = 0.90 + 0.2 * Math.random();
this.lastValue = this.lastValue * factor;
final Millisecond now = new Millisecond();
System.out.println("Now = " + now.toString());
this.series.add(new Millisecond(), this.lastValue);
}
public static void main(final String[] args) throws InterruptedException {
final DynamicDataDemo demo = new DynamicDataDemo("Dynamic Data Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
while(true){
demo.go();
Thread.currentThread().sleep(1000);
}
}
}
The example uses the default values specified in TimeSeries for the maximum item age and count. You'll want to change them to suit your requirements.
XML is fine, but it's voluminous for high rates; plan accordingly.
See also this example that uses javax.swing.Timer to avoid blocking the event dispatch thread.

Categories