Page 1 of 1

How to specify the coordinate origin

Posted: Thu Feb 27, 2014 2:08 am
by 17466735
Hello,

I wan to draw a graph whose left axis and bottom axis cross at point(15,10). Please refer to "correct.png".
However, when i use the following code, the axis of graph are not at the correct position. please refer to "wrong.png".

I create a method "setXYAxisCross" to implement the function.

do i misunderstand the use of
setPositionUnits();
setRelativePosition();

Code: Select all

import com.steema.teechart.BevelStyle;
import com.steema.teechart.PositionUnits;
import com.steema.teechart.Rectangle;
import com.steema.teechart.TChart;
import com.steema.teechart.axis.Axis;
import com.steema.teechart.drawing.Color;
import com.steema.teechart.editors.ChartEditor;
import com.steema.teechart.styles.Bar;
import com.steema.teechart.styles.HorizBar;
import com.steema.teechart.styles.Series;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import com.steema.teechart.styles.Box;
import com.steema.teechart.styles.Line;
import com.steema.teechart.styles.PointerStyle;
import com.steema.teechart.styles.VerticalAxis;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class TestAxisPosition {

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            Logger.getLogger(TestAxisPosition.class.getName()).log(Level.SEVERE, null, ex);
        }

        MenuFrame4 frame = new MenuFrame4();
        frame.initChart();
        frame.setVisible(true);


    }
}

class MenuFrame4 extends JFrame {

    public static final Color FEATURES_TREECOLOR = new Color(234, 238, 255);
    public static final String FEATURES_URL = "features/features.xml";
    public static final String NEW_FEATURES_URL = "features/new.xml";
    private TChart chart = null;

    public MenuFrame4() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        this.setTitle("test Teechart Axis position");


        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        this.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent we) {

                System.exit(0);
            }
        });

        this.setLayout(new BorderLayout());


        JMenuBar menuBar = new JMenuBar();
        JMenu opMenu = new JMenu("Operation");
        JMenuItem saveItem = new JMenuItem("save");
        JMenuItem openItem = new JMenuItem("load");
        JMenuItem editItem = new JMenuItem("edit");
        JMenuItem posItem = new JMenuItem("Axis position");
//        opMenu.add(saveItem);
//        opMenu.add(openItem);
        opMenu.add(editItem);
        opMenu.add(posItem);

        saveItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (chart != null) {
                    try {
                        chart.setText("Stored chart");
                        chart.getSeries(0).setTitle("StoreA");
                        chart.getSeries(1).setTitle("StoreB");
                        Axis bottom = chart.getAxes().getBottom();
                        System.out.println(bottom.getLabels().getItems().size());
                        bottom.getLabels().getItems().add((double) 0, "MyXLabel-1");
                        bottom.getLabels().getItems().add((double) 1, "MyXLabel-2");
                        chart.repaint();

                        JOptionPane.showMessageDialog(null, "Current tchart will be stored");
                        chart.getExport().getTemplate().toFile("c:\\tt23.tej");
                        chart.getExport().getTemplate().toXML("c:\\tt23.xml");
                        chart.getAspect().setView3D(true);
                        chart.setText("New Chart");
                        chart.getSeries(0).setTitle("new A");
                        chart.getSeries(1).setTitle("new B");
                        bottom = chart.getAxes().getBottom();
                        System.out.println(bottom.getLabels().getItems().size());
                        bottom.getLabels().getItems().clear();
                        bottom.getLabels().getItems().set(0, "new-1");
                        chart.repaint();
                    } catch (IOException ex) {
                        Logger.getLogger(MenuFrame2.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });

        openItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                chart = new TChart();
                if (chart != null) {
                    try {
//                        chart.getImport().getTemplate().fromFile("c:\\tt23.tej"); //load fail
                        chart.getImport().getTemplate().fromXML("c:\\tt23.xml");
                        chart.updateUI();
                        chart.repaint();

                    } catch (Exception ex) {
                        Logger.getLogger(MenuFrame2.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });


        editItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (chart != null) {
                    ChartEditor.editChart(chart.getChart());
                }
            }
        });

        posItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (chart != null) {
                    setXYAxisCross(chart, 15, 10); // 
                }
            }
        });


        menuBar.add(opMenu);

        setJMenuBar(menuBar);

        pack();
        this.setSize(600, 800);
        setLocationRelativeTo(null);
//        setVisible(true);
    }

    public void setXYAxisCross(TChart chart, int offset_x, int offset_y) {
        Axis bottom = chart.getAxes().getBottom();
        Axis left = chart.getAxes().getLeft();
        int y = left.calcPosValue(offset_y);
        bottom.setPositionUnits(PositionUnits.PIXELS);
        bottom.setRelativePosition(y);

        int x = bottom.calcPosValue(offset_x);
        left.setPositionUnits(PositionUnits.PIXELS);
        left.setRelativePosition(x);
    }

    public void initChart() {


        chart = new TChart();
        Line line = new Line(chart.getChart());
        line.add(2.0, 10);
        line.add(12.0, 13);
        line.add(31.0, 33);
        line.add(41.0, 49);
        line.add(52, 55);
        line.getPointer().setVisible(true);
        chart.getAxes().getLeft().setAutomatic(false);
        chart.getAxes().getLeft().setMaximum(60);
        chart.getAxes().getLeft().setMinimum(-5);
        chart.refreshControl();


        chart.setBounds(new Rectangle(0, 0, 500, 500));

        //设置图标的属性
        chart.getAspect().setView3D(false); // no 3D
        this.add(chart, BorderLayout.CENTER);

    }
}

Thanks.

Re: How to specify the coordinate origin

Posted: Thu Feb 27, 2014 2:11 am
by 17466735
i am sorry.

The wrong.png is here.

Re: How to specify the coordinate origin

Posted: Thu Feb 27, 2014 2:35 am
by 17466735
Luckily, i finally make it work correctly now. :)

// bottom.setRelativePosition(y);
bottom.setRelativePosition(left.iEndPos-y);

// left.setRelativePosition(x);
left.setRelativePosition(x-bottom.iStartPos);

I hope this poster will be useful for the guy who needs this function too.

I think how to specify the coordinate origin is a good function for the user.
will teechart add this function into source code?

Next step, when the axis is change to log or up and down, how can i catch these kinds of events to
refresh the coordinate origin?

Thanks.

Re: How to specify the coordinate origin

Posted: Thu Feb 27, 2014 2:58 am
by 17466735
I think the "addChartPaintListener()" will catch these events, e.g., the axis is upside down.
However, it fails. Do you have any idea?

Thanks.

Re: How to specify the coordinate origin

Posted: Fri Feb 28, 2014 12:13 pm
by yeray
Hello rubby,
rubby wrote:Luckily, i finally make it work correctly now. :)

// bottom.setRelativePosition(y);
bottom.setRelativePosition(left.iEndPos-y);

// left.setRelativePosition(x);
left.setRelativePosition(x-bottom.iStartPos);

I hope this poster will be useful for the guy who needs this function too.
I'm glad to hear you found it. And thanks for sharing.
rubby wrote:I think how to specify the coordinate origin is a good function for the user.
will teechart add this function into source code?
As you've seen it's not very difficult to calculate the pixel that corresponds to a given value and then to move the axis to that position. Being it quite easy we prefer to keep it as it is for the moment.

Let me explain the problematic if we implement what you suggest.
In your example you'll notice that, if you scroll or zoom in the chart, the axes remain in the same position, so they do not cross at the same values any more. This is the default behaviour of TeeChart. If you want the axes to always cross at the same coordinates, you/teechart will have to recalculate the axes positions in the scroll and zoom events. But then you'll be able to scroll the chart to a point that an axis will be out of the ChartRect, so you can find the case where you'll have a chart without axes visible quite easily, isn't it?
rubby wrote:Next step, when the axis is change to log or up and down, how can i catch these kinds of events to
refresh the coordinate origin?
rubby wrote:I think the "addChartPaintListener()" will catch these events, e.g., the axis is upside down.
However, it fails. Do you have any idea?
If you are changing the properties in code, you already know when this happens.
If you are looking for an event being fired when you change some property in the editor, I'm afraid there isn't such an event.
On the other hand, if you know what properties to check, you can check their values before and after opening the editor, and perform the desired actions then. Ie:

Code: Select all

        editItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (chart != null) {
                    //save properties to check
                    ChartEditor.editChart(chart.getChart());
                    //check if there are changes in the properties
                }
            }
        });

Re: How to specify the coordinate origin

Posted: Mon Mar 03, 2014 2:11 am
by 17466735
thanks for your reply. I'll have a try.