SWT stacked bar chart show total value

468 views Asked by At

I'm using SWT chart and my stacked bar chart looks like in the picture below. The bar char represents the response time for services. The dark blue bar represents the technical part of the response time and the light blue bar represents the think time.

enter image description here

My question is now if it is possible to show the total response time in the light blue bar instead of only the think time. For example in the third row the total response time would be 10952 ms (technical response time + think time), but it only shows the think time which is 10000 ms. Does anyone know of a way to implement this into the SWT bar chart? Thanks in advance for your help.

1

There are 1 answers

0
tzwickl On BEST ANSWER

OK I could now solve the problem by adding a SWT paint listener which draws a text for every bar:

plotArea.addListener(SWT.Paint, new Listener() {

        /**
         * The x-axis offset
         */
        private int xAxisOffset = 8;

        /**
         * The y-axis offset.
         */
        private int yAxisOffset = 3;

        @Override
        public void handleEvent(final Event event) {
            GC gc = event.gc;

            ISeries[] series = chart.getSeriesSet().getSeries();

            for (double xAxis : series[0].getXSeries()) {

                double techTime = series[0].getYSeries()[(int) xAxis];
                double thinkTime = series[1].getYSeries()[(int) xAxis];

                int totalTime = (int) Math.round(techTime + thinkTime);
                int xCoord = chart.getAxisSet().getXAxis(0).getPixelCoordinate(xAxis) - this.xAxisOffset;
                int yCoord = chart.getAxisSet().getYAxis(0).getPixelCoordinate(totalTime / this.yAxisOffset);

                gc.drawText(totalTime + " ms", yCoord, xCoord, SWT.DRAW_TRANSPARENT);
            }
        }
});

After that the SWT bar chart looks like this: enter image description here

Furthermore to show the values for the dark blue bar and the light blue bar separately, I added a listener which shows the respective value in a tooltip text when hovering over it with the mouse:

plotArea.addListener(SWT.MouseHover, new Listener() {

        @Override
        public void handleEvent(final Event event) {
            IAxis xAxis = chart.getAxisSet().getXAxis(0);
            IAxis yAxis = chart.getAxisSet().getYAxis(0);

            int x = (int) Math.round(xAxis.getDataCoordinate(event.y));
            double y = yAxis.getDataCoordinate(event.x);

            ISeries[] series = chart.getSeriesSet().getSeries();

            double currentY = 0.0;
            ISeries currentSeries = null;

            /* over all series */
            for (ISeries serie : series) {
                double[] yS = serie.getYSeries();

                if (x < yS.length && x >= 0) {
                    currentY += yS[x];
                    currentSeries = serie;

                    if (currentY > y) {
                        y = yS[x];
                        break;
                    }
                }
            }

            if (currentY >= y) {
                plotArea.setToolTipText(currentSeries.getDescription() + ": " + Math.round(y) + " ms");
            } else {
                plotArea.setToolTipText(null);
            }
        }
});