OpenFlashChart 最大值

  

    OpenFlashChart是一款基于flash的图表软件,它的最大优点是输出的图具有flash动态特性,用户比较乐于接受。

  

    目前2.0版中仍没有解决图表Y轴的最大值问题,下面把解决的办法贴出来。

 

    1. 先定义一个类Ymax,包含两个属性:最大值和步长

 

package org.wti.openflashchart;

public class Ymax {
	private int maxValue; // Y轴最大值
	private int stepValue; // Y轴步进值

	public int getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(int maxValue) {
		this.maxValue = maxValue;
	}

	public int getStepValue() {
		return stepValue;
	}

	public void setStepValue(int stepValue) {
		this.stepValue = stepValue;
	}

}

 

 

 

    2. 写个工具类,getYMax方法的输入为List<String>,输出为Ymax

 

 
package org.wti.openflashchart;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.util.StringUtils;

public class YmaxUtil {
	Ymax ymax;

	/**
	 * 在图表数据list中取出最大值
	 * 
	 * @param dataList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getMaxValue(List<String> dataList) {
		ContentComparator comp = new ContentComparator();
		Collections.sort(dataList, comp);
		for (int i = 0; i < dataList.size(); i++) {
			String content = dataList.get(i);
		}
		if (dataList.size() > 0)
			return dataList.get(dataList.size() - 1);
		else
			return "";
	}

	/**
	 * 计算出合适的Y轴最大值、步长
	 * 算法参考“http://forums.openflashchart.com/viewtopic.php?f=4&t=896&p=3572&hilit=axis#p3572”
	 * 
	 * @param max
	 * @return
	 */
	public Ymax getYMax(List<String> dataList) {

         // 先取出list最大值
		String strMax = this.getMaxValue(dataList); 
		if (!StringUtils.hasText(strMax)) {
			ymax.setMaxValue(20);
			ymax.setStepValue(5);
			return ymax;
		}

		double max = new Double(strMax);
		double orderOfMagnitude = Math.pow(10, Math.floor(Math.log(max) / Math.log(10)));
		double unit = Math.floor(max / orderOfMagnitude);
		double stepValue = orderOfMagnitude;

		if (unit < 2) {
			stepValue /= 2;
		} else {
			if (unit > 5)
				stepValue *= 2;
		}

		double maxValue = Math.ceil(max / stepValue) * stepValue;
		double steps = Math.ceil(max / stepValue);
		ymax.setMaxValue(new Double(maxValue).intValue());
		ymax.setStepValue(new Double(steps).intValue());
		return ymax;
	}

	public static void main(String args[]) {

		List<String> dataList = new ArrayList<String>();
		dataList.add("13");
		dataList.add("90");
		dataList.add("122");
		dataList.add("32");
		dataList.add("35");

		YmaxUtil ymaxUtil = new YmaxUtil();
		Ymax ymax = ymaxUtil.getYMax(dataList);

		System.out.println("Ymax==========" + ymax.getMaxValue()); 
		System.out.println("Step==========" + ymax.getStepValue()); 
	}

	public Ymax getYmax() {
		return ymax;
	}

	public void setYmax(Ymax ymax) {
		this.ymax = ymax;
	}

}

 

 

 

   

     3.  在输出图表的时,设置最大值及步长

 

Graph g = new Graph();
...
Ymax ymax = ymaxUtil.getYMax(DataList); // DataList是你的数据
g.set_y_max(ymax.getMaxValue());
g.y_label_steps(ymax.getStepValue());
...

 

    效果图如下:

    

OpenFlashChart 最大值_第1张图片

你可能感兴趣的:(算法,PHP,F#,Flash)