How to specify the coordinate origin
Posted: Thu Feb 27, 2014 2:08 am
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();
Thanks.
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.