Search in sources :

Example 1 with LabelFormat

use of org.eclipse.tracecompass.internal.tmf.chart.ui.format.LabelFormat in project tracecompass by tracecompass.

the class SwtScatterChart method refreshDisplayLabels.

@Override
protected void refreshDisplayLabels() {
    /**
     * TODO: support for the Y axis too
     */
    /* Only refresh if labels are visible */
    Chart chart = getChart();
    IAxisSet axisSet = chart.getAxisSet();
    IAxis xAxis = axisSet.getXAxis(0);
    if (!xAxis.getTick().isVisible()) {
        return;
    }
    /*
         * Shorten all the labels to 5 characters plus "…" when the longest
         * label length is more than 50% of the chart height.
         */
    Rectangle rect = chart.getClientArea();
    int lengthLimit = (int) (rect.height * 0.40);
    GC gc = new GC(getParent());
    gc.setFont(xAxis.getTick().getFont());
    // labels.
    if (!fXStringMap.isEmpty()) {
        /* Find the longest category string */
        String longestString = Collections.max(fXStringMap.keySet(), Comparator.comparingInt(String::length));
        /* Get the length and height of the longest label in pixels */
        Point pixels = gc.stringExtent(longestString);
        /* Completely arbitrary */
        int cutLen = 5;
        if (pixels.x > lengthLimit) {
            /* We have to cut down some strings */
            for (Entry<String, Integer> entry : fXStringMap.entrySet()) {
                String reference = checkNotNull(entry.getKey());
                if (reference.length() > cutLen) {
                    String key = reference.substring(0, cutLen) + ELLIPSIS;
                    fVisibleXMap.remove(reference);
                    fVisibleXMap.put(key, entry.getValue());
                } else {
                    fVisibleXMap.inverse().remove(entry.getValue());
                    fVisibleXMap.put(reference, entry.getValue());
                }
            }
        } else {
            /* All strings should fit */
            resetBiMap(fXStringMap, fVisibleXMap);
        }
        for (IAxis axis : axisSet.getXAxes()) {
            IAxisTick tick = axis.getTick();
            tick.setFormat(new LabelFormat(fVisibleXMap));
        }
    }
    /* Cleanup */
    gc.dispose();
}
Also used : IAxisTick(org.eclipse.swtchart.IAxisTick) LabelFormat(org.eclipse.tracecompass.internal.tmf.chart.ui.format.LabelFormat) IAxisSet(org.eclipse.swtchart.IAxisSet) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) GC(org.eclipse.swt.graphics.GC) Chart(org.eclipse.swtchart.Chart) IAxis(org.eclipse.swtchart.IAxis) Point(org.eclipse.swt.graphics.Point)

Example 2 with LabelFormat

use of org.eclipse.tracecompass.internal.tmf.chart.ui.format.LabelFormat in project tracecompass by tracecompass.

the class SwtScatterChart method configureAxes.

@Override
protected void configureAxes() {
    /* Format X axes */
    Stream.of(getChart().getAxisSet().getXAxes()).forEach(a -> {
        IAxisTick tick = checkNotNull(a.getTick());
        Format format;
        /* Give a continuous formatter if the descriptors are numericals */
        if (getXDescriptorsInfo().areNumerical()) {
            format = getContinuousAxisFormatter(fXRanges, getXDescriptorsInfo());
        } else {
            fVisibleXMap = HashBiMap.create(fXStringMap);
            format = new LabelFormat(fVisibleXMap);
            updateTickMark(fVisibleXMap, tick, getChart().getPlotArea().getSize().x);
        }
        tick.setFormat(format);
    });
    /* Format Y axes */
    Stream.of(getChart().getAxisSet().getYAxes()).forEach(a -> {
        IAxisTick tick = checkNotNull(a.getTick());
        Format format;
        /* Give a continuous formatter if the descriptors are numericals. */
        if (getYDescriptorsInfo().areNumerical()) {
            format = getContinuousAxisFormatter(fYRanges, getYDescriptorsInfo());
        } else {
            fVisibleYMap = HashBiMap.create(fYStringMap);
            format = new LabelFormat(fVisibleYMap);
            updateTickMark(fVisibleYMap, tick, getChart().getPlotArea().getSize().y);
        }
        tick.setFormat(format);
    });
}
Also used : IAxisTick(org.eclipse.swtchart.IAxisTick) Format(java.text.Format) LabelFormat(org.eclipse.tracecompass.internal.tmf.chart.ui.format.LabelFormat) LabelFormat(org.eclipse.tracecompass.internal.tmf.chart.ui.format.LabelFormat)

Aggregations

IAxisTick (org.eclipse.swtchart.IAxisTick)2 LabelFormat (org.eclipse.tracecompass.internal.tmf.chart.ui.format.LabelFormat)2 Format (java.text.Format)1 GC (org.eclipse.swt.graphics.GC)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 Chart (org.eclipse.swtchart.Chart)1 IAxis (org.eclipse.swtchart.IAxis)1 IAxisSet (org.eclipse.swtchart.IAxisSet)1