how to dissable x-axis line in jfreecharts - jfreechart

Im using jfreecharts for my application. I need to remove the x-axis line in the Line chart produced. How can i remove the axis line, Any help would be appreciated.

To remove your axis line and tick marks, refer this code.
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, "Line", "2009");
dataset.addValue(2.0, "Line", "2010");
dataset.addValue(null, "Line", "2011");
dataset.addValue(3.0, "Line", "2012");
final JFreeChart chart = ChartFactory.createLineChart(
"", // chart title
"", // domain axis label
"", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
false, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.WHITE);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeZeroBaselinePaint(Color.RED);
plot.setOutlineVisible(false);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainGridlinePaint(Color.BLUE);
final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
categoryAxis.setAxisLineVisible(false);
categoryAxis.setTickMarksVisible(false);
//categoryAxis.setVisible(false);
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//rangeAxis.setAxisLineVisible(false);
rangeAxis.setVisible(false);
rangeAxis.setLabelPaint(Color.BLUE);
rangeAxis.setRange(0, 3);
final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
//renderer.setItemLabelsVisible(false); //Deprecated.
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setSeriesStroke(0, new BasicStroke(2.0f));
Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
renderer.setSeriesShape(0, circle);
plot.getRenderer().setSeriesPaint(0, Color.BLUE);
try {
ChartUtilities.saveChartAsPNG(new File("E:\\jfreeLinechart.png"), chart, 211, 90);
System.out.println("=====chart=====");
} catch (Exception e) {
e.printStackTrace();
}
Also refer this link

Related

jfreechart multiple renderers tooltip not working

I have a figure with 2 sets of graphs (detections and localizations). First set (localizations) is red ,orange,pink and second set (detections) is blue,black,cyan. I have created a renderer for each set in order to set the colors. I have set the tooltip to true but when I mouseover on the second set (detection) I can't see the labels. I can see labels only for first set on mouseover(see picture) but not for the other set. Here is my code:
JFreeChart avg_chart = ChartFactory.createTimeSeriesChart(
"Average detections and localizations" ,
"" ,
"" ,
null ,
true , true , false);
avg_chart.setBackgroundPaint(Color.WHITE);
final XYPlot plot = avg_chart.getXYPlot( );
plot.setDataset(0,this.dataset_local);
plot.setDataset(1,this.dataset_detect);
plot.setRangeAxis(0,new NumberAxis("Localizations"));
plot.setRangeAxis(1,new NumberAxis("Detections"));
plot.mapDatasetToRangeAxis(0, 0);
plot.mapDatasetToRangeAxis(1, 1);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYLineAndShapeRenderer renderer1 = (XYLineAndShapeRenderer) plot.getRenderer(0);//localization
renderer1.setSeriesPaint( 0 , Color.RED );
renderer1.setSeriesPaint( 1 , Color.MAGENTA );
renderer1.setSeriesPaint( 2 , Color.orange );
renderer1.setBaseItemLabelsVisible(true);
XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true, false); //detection ****************
renderer2.setSeriesPaint( 0 , Color.BLUE);
renderer2.setSeriesPaint( 1 , Color.BLACK );
renderer2.setSeriesPaint( 2 , Color.CYAN );
renderer2.setBaseItemLabelsVisible(true);
plot.setRenderer(0,renderer1);
plot.setRenderer(1,renderer2);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd/MM/yyyy"));
return avg_chart;
}
I have tried XYLineAndShapeRenderer renderer2 = (XYLineAndShapeRenderer) plot.getRenderer(1) but it gives be a nulll exception.
ChartFactory.createTimeSeriesChart() adds an XYToolTipGenerator to renderer1 for you when tooltips is true. You probably just need to use it with renderer2:
renderer2.setBaseToolTipGenerator(renderer1.getBaseToolTipGenerator());
Or you can add a new one to renderer2:
XYToolTipGenerator toolTipGenerator2 = StandardXYToolTipGenerator.getTimeSeriesInstance();
renderer2.setBaseToolTipGenerator(toolTipGenerator2);

Codename one - autocompletetextfield getitem selected

I have two problems:
1) First, I want to add a map in AutoCompleteTextField exactlly in DefaultListModel and after I want to add the the listmodel in my autocompletetextField
2) How can I get the text when I select an element in the AutoCompleteTextField?
Form hi = new Form("Auto Complete", new BoxLayout(BoxLayout.Y_AXIS));
AutoCompleteTextField ac = new AutoCompleteTextField("Short", "Shock", "Sholder", "Shrek0", "3asna", "niazra");
ac.setMinimumElementsShownInPopup(5);
//final DefaultListModel<Map<String,Object>> options = new DefaultListModel<>();
final DefaultListModel<String> options = new DefaultListModel<>();
AutoCompleteTextField an = new AutoCompleteTextField(options);
hi.add(an);
ac.addListListener(a -> {
List<Object> ls = new List<>();
System.out.println("i want to display the text selected");
});
hi.add(ac);
hi.show();
When you select an item in the suggestion box of an AutoCompleteTextField the text of this item is copied to the TextField part of the AutoCompleteTextfield, but this occur only after the ListEvent.
In order to have this behavior, prefer using a Selection Listener on the DefaultListModel:
Form hi = new Form("ACTF", new BoxLayout(BoxLayout.Y_AXIS));
DefaultListModel<String> defList = new DefaultListModel<>("Red", "Green", "Blue", "Orange");
AutoCompleteTextField tf1 = new AutoCompleteTextField(defList);
defList.addSelectionListener((oldid, newid)-> Log.p(defList.getItemAt(newid)));
hi.add(tf1);
hi.show();
I don't know why, it occur two times after showing the form, but it works perfectly fine after.
Edit: If you want to display the text on screen, you must use something like this :
Form hi = new Form("ACTF", new BoxLayout(BoxLayout.Y_AXIS));
Label text = new Label("Selected text");
DefaultListModel<String> defList = new DefaultListModel<>("Red", "Green", "Blue", "Orange");
AutoCompleteTextField tf1 = new AutoCompleteTextField(defList);
defList.addSelectionListener((oldid, newid)-> {
text.setText(defList.getItemAt(newid));
hi.revalidate();
});
hi.add(text);
hi.add(tf1);
hi.show();
EDIT 2: Example with a linked map:
Form hi = new Form("ACTF", new BoxLayout(BoxLayout.Y_AXIS));
Map testMap = new HashMap<String, String>();
testMap.put("Red", "Roses are red");
testMap.put("Green", "Grass is green");
testMap.put("Blue", "Sky is blue");
testMap.put("Orange", "Apricots are orange");
Label text = new Label("Selected text");
DefaultListModel<String> defList = new DefaultListModel<>(testMap.keySet());
AutoCompleteTextField tf1 = new AutoCompleteTextField(defList);
defList.addSelectionListener((oldid, newid)-> {
text.setText((String) testMap.get(defList.getItemAt(newid)));
hi.revalidate();
});
hi.add(text);
hi.add(tf1);
hi.show();

Add text to picture in Windows Universal app

I have universal app created for Win 10 mobile which captures photo. I want to add stamp (some text) to my photo. Is it possible? It seems working with bitmap is really difficult in universal applications
Check this link:
How to add wartermark text/image to a bitmap in Windows Store app
Or better use Win2D library (you can find it on NuGet) and snippet like this:
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget offscreen = new CanvasRenderTarget(device, 500, 500, 96);
cbi = await CanvasBitmap.LoadAsync(device, "mydog.jpg");
using (var ds = offscreen.CreateDrawingSession())
{
ds.DrawImage(cbi);
var format = new CanvasTextFormat()
{
FontSize = 24,
HorizontalAlignment = CanvasHorizontalAlignment.Left,
VerticalAlignment = CanvasVerticalAlignment.Top,
WordWrapping = CanvasWordWrapping.Wrap,
FontFamily = "Tahoma"
};
var tl = new CanvasTextLayout(ds, "Some text", format, 200, 50); // width 200 and height 50
ds.DrawTextLayout(tl, 10, 10, Colors.Black);
tl.Dispose();
}
using (var stream = new InMemoryRandomAccessStream())
{
stream.Seek(0);
await offscreen.SaveAsync(stream, CanvasBitmapFileFormat.Png);
BitmapImage image = new BitmapImage();
image.SetSource(stream);
img.Source = image;
}

Area chart in JFreeChart includes zero

Here is how my area chart in JFreeChart looks like. The area chart starts from 0 and rises up to the first value 5 or 1. Also after 400 on the y axis it falls down back to zero. I want it to start from 5 or 1 and not fall back to 0 after 400.
Area Chart http://imageshack.com/a/img837/6572/2ab0.jpg
Given below is the code. How do I fix it to avoid the zero?
package com.ebay.aerohc.benchmark;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.VerticalAlignment;
import org.jfree.util.Log;
import org.jfree.util.PrintStreamLogTarget;
/**
* A simple demonstration application showing how to create an area chart using data from a
* {#link CategoryDataset}.
*/
public class AreaChartDemo extends ApplicationFrame {
/**
* Creates a new demo application.
*
* #param title the frame title.
*/
public AreaChartDemo(final String title) {
super(title);
// create a dataset...
final double[][] data = new double[][] {
{1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
{5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0}/*,
{4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}*/
};
final CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
new String[]{"AeroHC","KernelAHC"}, new String[]{"50","100","150","200","250","300","350","400"}, data
);
// create the chart...
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
chartPanel.setEnforceFileExtensions(false);
setContentPane(chartPanel);
}
// ****************************************************************************
// * JFREECHART DEVELOPER GUIDE *
// * The JFreeChart Developer Guide, written by David Gilbert, is available *
// * to purchase from Object Refinery Limited: *
// * *
// * http://www.object-refinery.com/jfreechart/guide.html *
// * *
// * Sales are used to provide funding for the JFreeChart project - please *
// * support us so that we can continue developing free software. *
// ****************************************************************************
/**
* Creates a chart.
*
* #param dataset the dataset.
*
* #return The chart.
*/
private JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createAreaChart(
"Area Chart", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
// final StandardLegend legend = (StandardLegend) chart.getLegend();
// legend.setAnchor(StandardLegend.SOUTH);
chart.setBackgroundPaint(Color.white);
final TextTitle subtitle = new TextTitle("An area chart demonstration. We use this "
+ "subtitle as an example of what happens when you get a really long title or "
+ "subtitle.");
subtitle.setFont(new Font("SansSerif", Font.PLAIN, 12));
subtitle.setPosition(RectangleEdge.TOP);
// subtitle.setSpacer(new Spacer(Spacer.RELATIVE, 0.05, 0.05, 0.05, 0.05));
subtitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
chart.addSubtitle(subtitle);
final CategoryPlot plot = chart.getCategoryPlot();
plot.setForegroundAlpha(0.5f);
// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
domainAxis.setLowerMargin(0.1);
domainAxis.setUpperMargin(0.5);
domainAxis.addCategoryLabelToolTip("Type 1", "The first type.");
domainAxis.addCategoryLabelToolTip("Type 2", "The second type.");
domainAxis.addCategoryLabelToolTip("Type 3", "The third type.");
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setLabelAngle(0 * Math.PI / 2.0);
rangeAxis.setAutoRangeIncludesZero(false);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
/**
* Starting point for the demonstration application.
*
* #param args ignored.
*/
public static void main(final String[] args) {
Log.getInstance().addTarget(new PrintStreamLogTarget());
final AreaChartDemo demo = new AreaChartDemo("Area Chart Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
You can configure this in the renderer:
AreaRenderer renderer = (AreaRenderer) plot.getRenderer();
renderer.setEndType(AreaRendererEndType.LEVEL);
Use an XYSeriesCollection for your dataset and switch to using ChartFactory#createXYAreaChart.
You will need to change some of the configuration as you will not be using a CategoryAxis
Not enough rep to comment on David Gilbert's answer, so I have to post a separate answer.
I had the desired results when configuring the truncate end type in the renderer:
AreaRenderer renderer = (AreaRenderer) plot.getRenderer();
renderer.setEndType(AreaRendererEndType.TRUNCATE);

Rotate a Geometry Path

I'm working newly with a Streamgeometry to draw a simple arrow. Now I need to turn the arrow to a specified angle. But how to rotate this geometry?
Dim pt1 As New Point(X1, Me.Y1) 'left point
Dim pt2 As New Point(_X2, Me.Y2) 'right point
Dim pt3 As New Point(_X2 + (HeadWidth * cost - HeadHeight * sint), Y2 + (HeadWidth * sint + HeadHeight * cost)) 'arrow line down
Dim pt4 As New Point(_X2 + (HeadWidth * cost + HeadHeight * sint), Y2 - (HeadHeight * cost - HeadWidth * sint)) 'arrow line up
context.BeginFigure(pt1, True, False)
context.LineTo(pt2, True, True)
context.LineTo(pt3, True, True)
context.LineTo(pt2, True, True)
context.LineTo(pt4, True, True)
If the rotation is only for presentation (i.e. you don't care that the original geometry data is still an arrow pointing in the original direction) then you can apply a transform to it.
After you've drawn on your context, just apply the transform on the original StreamGeometry object (code in C# but it applies to VB.NET too):
var geo = new StreamGeometry();
using (var ctx = geo.Open())
{
ctx.BeginFigure(new Point(0, 20), false, false);
ctx.LineTo(new Point(100, 20), true, true);
ctx.LineTo(new Point(80, 40), true, true);
ctx.LineTo(new Point(80, 0), true, true);
ctx.LineTo(new Point(100, 20), true, true);
}
geo.Transform = new RotateTransform(45);
var drawing = new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.Black, 1), geo);
image1.Source = new DrawingImage(drawing);
The above code will draw an arrow pointing down/right on an Image control named image1.

Resources