Search in sources :

Example 1 with Node

use of org.eclipse.swtchart.model.Node in project portfolio by buchen.

the class PieChart method renderLabels.

protected void renderLabels(PaintEvent e) {
    for (ISeries<?> series : getSeriesSet().getSeries()) {
        if (series instanceof ICircularSeries) {
            IAxis xAxis = getAxisSet().getXAxis(series.getXAxisId());
            IAxis yAxis = getAxisSet().getYAxis(series.getYAxisId());
            List<Node> nodes = ((ICircularSeries<?>) series).getRootNode().getChildren();
            if (!nodes.isEmpty()) {
                for (Node node : nodes) {
                    renderNodeLabel(node, (ICircularSeries<?>) series, e.gc, xAxis, yAxis);
                }
            }
        }
    }
}
Also used : Node(org.eclipse.swtchart.model.Node) ICircularSeries(org.eclipse.swtchart.ICircularSeries) IAxis(org.eclipse.swtchart.IAxis)

Example 2 with Node

use of org.eclipse.swtchart.model.Node in project portfolio by buchen.

the class PieChart method getNodeAt.

/**
 * Find the node at position
 */
public Node getNodeAt(int x, int y) {
    Node node = null;
    for (ISeries<?> series : getSeriesSet().getSeries()) {
        if (!(series instanceof ICircularSeries)) {
            continue;
        }
        ICircularSeries<?> circularSeries = (ICircularSeries<?>) series;
        double primaryValueX = getSelectedPrimaryAxisValue(x, PieChart.Orientation.X_AXIS);
        double primaryValueY = getSelectedPrimaryAxisValue(y, PieChart.Orientation.Y_AXIS);
        node = circularSeries.getPieSliceFromPosition(primaryValueX, primaryValueY);
        circularSeries.setHighlightedNode(node);
    }
    return node;
}
Also used : Node(org.eclipse.swtchart.model.Node) ICircularSeries(org.eclipse.swtchart.ICircularSeries)

Example 3 with Node

use of org.eclipse.swtchart.model.Node in project portfolio by buchen.

the class PieChart method renderNodeLabel.

protected void renderNodeLabel(Node node, ICircularSeries<?> series, GC gc, IAxis xAxis, IAxis yAxis) {
    // children drawn first as parent overrides it's section of drawing
    if (!node.getChildren().isEmpty()) {
        for (Node childNode : node.getChildren()) {
            renderNodeLabel(childNode, series, gc, xAxis, yAxis);
        }
    }
    if (!node.isVisible())
        return;
    int level = node.getLevel() - series.getRootNode().getLevel() + (chartType == IPieChart.ChartType.PIE ? 0 : 1);
    Font oldFont = gc.getFont();
    gc.setForeground(Colors.WHITE);
    int angleStart = node.getAngleBounds().x;
    int angleWidth = (int) (node.getAngleBounds().y * 0.5);
    Point outerEnd = calcPixelCoord(xAxis, yAxis, level * 0.85, angleStart + angleWidth);
    FontDescriptor boldDescriptor = FontDescriptor.createFrom(gc.getFont()).setHeight(9);
    gc.setFont(boldDescriptor.createFont(getDisplay()));
    String label = labelProvider.getLabel(node);
    if (label != null) {
        Point textSize = gc.textExtent(label);
        gc.drawString(label, outerEnd.x - (textSize.x / 2), outerEnd.y - (textSize.y / 2), true);
    }
    gc.setFont(oldFont);
}
Also used : Node(org.eclipse.swtchart.model.Node) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) FontDescriptor(org.eclipse.jface.resource.FontDescriptor)

Example 4 with Node

use of org.eclipse.swtchart.model.Node in project portfolio by buchen.

the class HoldingsPieChartSWT method createControl.

@Override
public Control createControl(Composite parent) {
    chart = new PieChart(parent, IPieChart.ChartType.DONUT, this::getNodeLabel);
    // set customized tooltip builder
    chart.getToolTip().setToolTipBuilder((container, currentNode) -> {
        RowLayout layout = new RowLayout(SWT.VERTICAL);
        layout.center = true;
        container.setLayout(layout);
        Composite data = new Composite(container, SWT.NONE);
        GridLayoutFactory.swtDefaults().numColumns(2).applyTo(data);
        Label assetLabel = new Label(data, SWT.NONE);
        FontDescriptor boldDescriptor = FontDescriptor.createFrom(assetLabel.getFont()).setStyle(SWT.BOLD);
        assetLabel.setFont(boldDescriptor.createFont(assetLabel.getDisplay()));
        assetLabel.setText(currentNode.getId());
        NodeData nodeData = nodeDataMap.get(currentNode.getId());
        if (nodeData != null) {
            Label right = new Label(data, SWT.NONE);
            // $NON-NLS-1$ //$NON-NLS-2$
            right.setText("(" + nodeData.percentageString + ")");
            Label info = new Label(container, SWT.NONE);
            info.setText(// $NON-NLS-1$
            String.format(// $NON-NLS-1$
            "%s x %s = %s", nodeData.shares, nodeData.valueSingle, nodeData.value));
        }
    });
    // Listen on mouse clicks to update information pane
    ((Composite) chart.getPlotArea()).addListener(SWT.MouseUp, event -> {
        Node node = chart.getNodeAt(event.x, event.y);
        if (node == null)
            return;
        NodeData nodeData = nodeDataMap.get(node.getId());
        if (nodeData != null) {
            financeView.setInformationPaneInput(nodeData.position.getInvestmentVehicle());
        }
    });
    chart.getTitle().setVisible(false);
    chart.getLegend().setPosition(SWT.RIGHT);
    updateChart();
    return chart;
}
Also used : PieChart(name.abuchen.portfolio.ui.util.chart.PieChart) IPieChart(name.abuchen.portfolio.ui.views.IPieChart) Composite(org.eclipse.swt.widgets.Composite) RowLayout(org.eclipse.swt.layout.RowLayout) Node(org.eclipse.swtchart.model.Node) Label(org.eclipse.swt.widgets.Label) FontDescriptor(org.eclipse.jface.resource.FontDescriptor)

Example 5 with Node

use of org.eclipse.swtchart.model.Node in project portfolio by buchen.

the class HoldingsPieChartSWT method updateChart.

private void updateChart() {
    List<Double> values = new ArrayList<>();
    List<String> labels = new ArrayList<>();
    nodeDataMap.clear();
    // 
    snapshot.getAssetPositions().filter(// 
    p -> p.getValuation().getAmount() > 0).sorted(// 
    (l, r) -> Long.compare(r.getValuation().getAmount(), l.getValuation().getAmount())).forEach(p -> {
        String nodeId = JSONObject.escape(p.getDescription());
        labels.add(nodeId);
        values.add(p.getValuation().getAmount() / Values.Amount.divider());
        NodeData data = new NodeData();
        data.position = p;
        data.percentage = p.getShare();
        data.percentageString = Values.Percent2.format(p.getShare());
        data.shares = Values.Share.format(p.getPosition().getShares());
        data.value = Values.Money.format(p.getValuation());
        data.valueSingle = Values.Money.format(p.getValuation().multiply((long) Values.Share.divider()).divide(p.getPosition().getShares()));
        nodeDataMap.put(nodeId, data);
    });
    ICircularSeries<?> circularSeries = (ICircularSeries<?>) chart.getSeriesSet().getSeries(Messages.LabelStatementOfAssetsHoldings);
    if (circularSeries == null) {
        circularSeries = createSeries(values, labels);
    } else {
        if (hasDataSetChanged(labels)) {
            // refresh values only for smoother update
            ListIterator<String> iter = labels.listIterator();
            while (iter.hasNext()) {
                int idx = iter.nextIndex();
                String label = iter.next();
                Node node = circularSeries.getNodeById(label);
                if (node != null) {
                    node.setValue(values.get(idx));
                }
            }
        } else {
            circularSeries = createSeries(values, labels);
        }
    }
    setColors(circularSeries, values.size());
    chart.redraw();
}
Also used : Values(name.abuchen.portfolio.money.Values) ListIterator(java.util.ListIterator) AssetPosition(name.abuchen.portfolio.snapshot.AssetPosition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Composite(org.eclipse.swt.widgets.Composite) Messages(name.abuchen.portfolio.ui.Messages) Map(java.util.Map) AbstractFinanceView(name.abuchen.portfolio.ui.editor.AbstractFinanceView) PieChart(name.abuchen.portfolio.ui.util.chart.PieChart) FontDescriptor(org.eclipse.jface.resource.FontDescriptor) Node(org.eclipse.swtchart.model.Node) ICircularSeries(org.eclipse.swtchart.ICircularSeries) GridLayoutFactory(org.eclipse.jface.layout.GridLayoutFactory) List(java.util.List) RowLayout(org.eclipse.swt.layout.RowLayout) JSONObject(org.json.simple.JSONObject) Colors(name.abuchen.portfolio.ui.util.Colors) Color(org.eclipse.swt.graphics.Color) SWT(org.eclipse.swt.SWT) SeriesType(org.eclipse.swtchart.ISeries.SeriesType) IPieChart(name.abuchen.portfolio.ui.views.IPieChart) Collections(java.util.Collections) Label(org.eclipse.swt.widgets.Label) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) Control(org.eclipse.swt.widgets.Control) Node(org.eclipse.swtchart.model.Node) ArrayList(java.util.ArrayList) ICircularSeries(org.eclipse.swtchart.ICircularSeries)

Aggregations

Node (org.eclipse.swtchart.model.Node)11 Composite (org.eclipse.swt.widgets.Composite)5 ICircularSeries (org.eclipse.swtchart.ICircularSeries)5 PieChart (name.abuchen.portfolio.ui.util.chart.PieChart)4 IPieChart (name.abuchen.portfolio.ui.views.IPieChart)4 Color (org.eclipse.swt.graphics.Color)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 FontDescriptor (org.eclipse.jface.resource.FontDescriptor)3 RowLayout (org.eclipse.swt.layout.RowLayout)2 Label (org.eclipse.swt.widgets.Label)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Money (name.abuchen.portfolio.money.Money)1 Values (name.abuchen.portfolio.money.Values)1 AssetPosition (name.abuchen.portfolio.snapshot.AssetPosition)1 ClientSnapshot (name.abuchen.portfolio.snapshot.ClientSnapshot)1 Messages (name.abuchen.portfolio.ui.Messages)1