Page 1 of 1

Android - Custom Drawing.

Posted: Thu Sep 04, 2014 11:27 am
by 17070078
Hello.

I need help with custom chart drawing. What I have now:
chart.png
chart.png (14.94 KiB) Viewed 40466 times
Bars show count of sold goods and red triangle shows target count. Code looks like this:

Code: Select all

chart.addChartPaintListener(new ChartPaintAdapter() {
                @Override
                public void chartPainted(ChartDrawEvent chartDrawEvent) {
                    final int target = 300;
                    IGraphics3D graphics = holder.chart.getGraphics3D();
                    graphics.getPen().setColor(Color.black);
                    graphics.getBrush().setColor(Color.red);
                    Point point0 = new Point(target+10, graphics.getYCenter()*2);
                    Point point1 = new Point(target-10, graphics.getYCenter()*2);
                    Point point2 = new Point(target, graphics.getYCenter()*2 - 20);
                    graphics.triangle(point0, point1, point2, 0);
                }
            });
As you can see target equals 300, but on the chart it near 1000. Seems point.x is not value showed on X axis. But what is correct way to calculate the triangle position?
Also Y coordinate of triangle base is not correct to. How to put it exactly on X axis?

Thank you and best regards.

Re: Android - Custom Drawing.

Posted: Fri Sep 05, 2014 8:22 am
by yeray
Hello,
roman wrote:As you can see target equals 300, but on the chart it near 1000. Seems point.x is not value showed on X axis. But what is correct way to calculate the triangle position?
If 300 is an axis value, you have to transform it to screen pixels to use drawing techniques. The axes' calcPosValue() functions do this.
roman wrote:Also Y coordinate of triangle base is not correct to. How to put it exactly on X axis?
You could use the axis getPosition() method to get it.

This seems to work fine for me here:

Code: Select all

        chart.addChartPaintListener(new ChartPaintAdapter() {
                @Override
                public void chartPainted(ChartDrawEvent chartDrawEvent) {
                    final int target = tChart1.getAxes().getBottom().calcPosValue(300);
                    final int posAxis = tChart1.getAxes().getBottom().getPosition();
                    IGraphics3D graphics = tChart1.getGraphics3D();
                    graphics.getPen().setColor(Color.black);
                    graphics.getBrush().setColor(Color.red);
                    Point point0 = new Point(target+10, posAxis);
                    Point point1 = new Point(target-10, posAxis);
                    Point point2 = new Point(target, posAxis - 20);
                    graphics.triangle(point0, point1, point2, 0);
                }

Re: Android - Custom Drawing.

Posted: Fri Sep 05, 2014 10:35 am
by 17070078
Perfect, thanks.