1

I'm using JAVA, SWT and SWTChart to visualize a dive profile. To visualize the dive profile in the right shape, I use negative depths values, which is necessary but not correct. Thus I would like to remove or reverse the sign at the y axis caption.

Here is an example:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.swtchart.Chart;
import org.swtchart.ILineSeries;
import org.swtchart.LineStyle;
import org.swtchart.ISeries.SeriesType;
/**
* An example for line chart.
*/
public class ScatterChartExample {
private static final double[] xSeries = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
private static final double[] ySeries = { 0, -1.3, -2.0, -3.9, -5.6, -4.1, -5.3, -7.0, -3.9, -3.6, -1.1, 0};
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Dive Profile");
shell.setSize(500, 400);
shell.setLayout(new FillLayout());
createChart(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* create the chart.
*
* @param parent
* The parent composite
* @return The created chart
*/
static public Chart createChart(Composite parent) {
// create a chart
Chart chart = new Chart(parent, SWT.NONE);
// set titles
chart.getTitle().setText("Dive profile");
chart.getAxisSet().getXAxis(0).getTitle().setText("Time");
chart.getAxisSet().getYAxis(0).getTitle().setText("Depth");
// create scatter series
ILineSeries series = (ILineSeries) chart.getSeriesSet()
.createSeries(SeriesType.LINE, "series");
series.setLineStyle(LineStyle.SOLID);
series.enableArea(true);
series.setXSeries(xSeries);
series.setYSeries(ySeries);
// adjust the axis range
chart.getAxisSet().adjustRange();
return chart;
}
}

Any ideas???

Regards

  • Looked through the documentation of SWTChart and couldn't find anything that let's you add a label provider or similar. The closest thing I found was `IAxisTick#setFormat(Format)` which lets you set a `DecimalFormat`. However, there is no way I know of to tell the decimal format to omit the sign ("+" or "-"). What exactly bothers you about the minus sign? – Baz Aug 19 '14 at 16:58
  • You **could** create a font where the minus sign is empty and use that with `IAxisTick#setFont(Font)`, but that strikes me as quite a hack. – Baz Aug 19 '14 at 17:22
  • Well, a depth value is positive. Thus a negative value is by definition wrong. I will try the font hack, if no other idea pops up. I will post the result... – sharkscream Aug 20 '14 at 06:58
  • You can always change the axis title to something that works with negative values. – Baz Aug 20 '14 at 07:00
  • Do you mind if I already post all that as an answer? Then you can decide if you want to accept it. – Baz Aug 20 '14 at 07:45

1 Answers1

0

Ok, here is a solution that uses a modified version of the font "Arial". I just deleted the "-" character.

This is the code:

private static final double[] xSeries = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
private static final double[] ySeries = { 0, -1.3, -2.0, -3.9, -5.6, -4.1, -5.3, -7.0, -3.9, -3.6, -1.1, 0 };

private static Font           font;

/**
 * The main method.
 *
 * @param args
 *            the arguments
 */
public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Dive Profile");
    shell.setSize(500, 400);
    shell.setLayout(new FillLayout());

    display.loadFont("baz.ttf");

    font = new Font(display, "Baz", 12, SWT.NONE);

    createChart(shell);

    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();

    font.dispose();
}

/**
 * create the chart.
 *
 * @param parent
 *            The parent composite
 * @return The created chart
 */
static public Chart createChart(Composite parent)
{
    // create a chart
    Chart chart = new Chart(parent, SWT.NONE);
    // set titles
    chart.getTitle().setText("Dive profile");
    chart.getAxisSet().getXAxis(0).getTitle().setText("Time");
    chart.getAxisSet().getYAxis(0).getTitle().setText("Depth");
    chart.getAxisSet().getYAxis(0).getTick().setFont(font);
    chart.getAxisSet().getXAxis(0).getTick().setFont(font);
    // create scatter series
    ILineSeries series = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, "series");
    series.setLineStyle(LineStyle.SOLID);
    series.enableArea(true);
    series.setXSeries(xSeries);
    series.setYSeries(ySeries);
    // adjust the axis range
    chart.getAxisSet().adjustRange();
    return chart;
}

And this is the font (you can create your own if you want):

https://dl.dropboxusercontent.com/u/498403/baz.ttf

Looks like this:

enter image description here


Update

As an alternative, you can always use JFreeChart in SWT using a ChartComposite. Her is a tutorial.

Baz
  • 36,440
  • 11
  • 68
  • 94
  • The above solution works fine for me... I have integrated the font into the .jar file. The only problem now is getting the path from the .ttf file in the .jar file, since loadFont() does not like inputstreams. As workaround I generated a temporary copy of the file. – sharkscream Aug 20 '14 at 10:46
  • @sharkscream Yeah, sounds like the best solution. If you're happy with the end result, please consider accepting my answer. – Baz Aug 20 '14 at 10:52