use of org.eclipse.swtchart.IAxis in project tracecompass by tracecompass.
the class SwtBarChart method refreshDisplayLabels.
@Override
protected void refreshDisplayLabels() {
String @Nullable [] categories = fCategories;
/* Only if we have at least 1 category */
if (categories == null || categories.length == 0) {
return;
}
/* Only refresh if labels are visible */
IAxis xAxis = getChart().getAxisSet().getXAxis(0);
if (!xAxis.getTick().isVisible() || !xAxis.isCategoryEnabled()) {
return;
}
/*
* Shorten all the labels to 5 characters plus "…" when the longest
* label length is more than a percentage of the chart height.
*/
Rectangle rect = getChart().getClientArea();
int lengthLimit = (int) (rect.height * LENGTH_LIMIT);
GC gc = new GC(getParent());
gc.setFont(xAxis.getTick().getFont());
/* Find the longest category string */
String longestString = Arrays.stream(categories).max(Comparator.comparingInt(String::length)).orElse(categories[0]);
/* Get the length and height of the longest label in pixels */
Point pixels = gc.stringExtent(longestString);
/* Completely arbitrary */
int cutLen = 5;
String[] displayCategories = new String[categories.length];
if (pixels.x > lengthLimit) {
/* We have to cut down some strings */
for (int i = 0; i < categories.length; i++) {
if (categories[i].length() > cutLen) {
displayCategories[i] = categories[i].substring(0, cutLen) + ELLIPSIS;
} else {
displayCategories[i] = categories[i];
}
}
} else {
/* All strings should fit */
displayCategories = Arrays.copyOf(categories, categories.length);
}
xAxis.setCategorySeries(displayCategories);
/* Cleanup */
gc.dispose();
}
use of org.eclipse.swtchart.IAxis 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();
}
use of org.eclipse.swtchart.IAxis in project tracecompass by tracecompass.
the class SwtXYChartViewer method populate.
/**
* This method is called after the constructor by the factory constructor.
* While everything could be put in the constructor directly, splitting the
* constructor simply makes the code cleaner by reducing the number of null
* checks.
*/
protected final void populate() {
/* Create the consumer for the data */
XYChartConsumer chartConsumer = createChartConsumer();
fChartConsumer = chartConsumer;
/* Process all the objects from the stream of data */
fData.getDataProvider().getSource().forEach(chartConsumer::accept);
chartConsumer.finish();
/* Create the SWT series */
createChartSeries();
configureSeries(fObjectMap);
/* Adjust the chart range */
getChart().getAxisSet().adjustRange();
/* Configure axes */
configureAxes();
Arrays.stream(getChart().getAxisSet().getXAxes()).forEach(a -> a.enableLogScale(getModel().isXLogscale()));
Arrays.stream(getChart().getAxisSet().getYAxes()).forEach(a -> a.enableLogScale(getModel().isYLogscale()));
for (IAxis yAxis : getChart().getAxisSet().getYAxes()) {
/*
* SWTChart workaround: SWTChart fiddles with tick mark visibility
* based on the fact that it can parse the label to double or not.
*
* If the label happens to be a double, it checks for the presence
* of that value in its own tick labels to decide if it should add
* it or not. If it happens that the parsed value is already present
* in its map, the tick gets a visibility of false.
*
* The X axis does not have this problem since SWTCHART checks on
* label angle, and if it is != 0 simply does no logic regarding
* visibility. So simply set a label angle of 1 to the axis.
*/
yAxis.getTick().setTickLabelAngle(1);
}
/* Update the titles */
fXTitle = generateTitle(getXDescriptors(), getChart().getAxisSet().getXAxis(0));
fYTitle = generateTitle(getYDescriptors(), getChart().getAxisSet().getYAxis(0));
/* Refresh the titles to fit the current chart size */
refreshDisplayTitles();
refreshDisplayLabels();
}
use of org.eclipse.swtchart.IAxis in project olca-app by GreenDelta.
the class ContributionChart method setYRange.
private void setYRange(List<Contribution<?>> top, double rest) {
double min = rest < 0 ? rest : 0;
double max = rest > 0 ? rest : 0;
for (Contribution<?> item : top) {
min = Math.min(min, item.amount);
max = Math.max(max, item.amount);
}
double[] range = yrange(min, max);
IAxis y = chart.getAxisSet().getYAxis(0);
y.setRange(new Range(range[0], range[1]));
y.getTick().setTickMarkStepHint(10);
}
Aggregations