package com.dianping.cat.report.page;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PieChart {

	private String m_title;

	private List<Item> m_items = new ArrayList<Item>();

	private transient int m_size = 10;

	public PieChart setTitle(String title) {
		m_title = title;
		return this;
	}

	public String getTitle() {
   	return m_title;
   }

	public void addItems(List<Item> items) {
		Collections.sort(items, new ItemCompartor());
		int size = items.size();

		if (size <= m_size) {
			m_items = items;
		} else {
			for (int i = 0; i < m_size; i++) {
				m_items.add(items.get(i));
			}
			Item item = new Item().setTitle("Other");

			double sum = 0;
			for (int i = m_size; i < size; i++) {
				Item temp = items.get(i);

				sum += temp.getNumber();
			}
			m_items.add(item.setNumber(sum));
		}
	}

	public List<Item> getItems() {
		return m_items;
	}

	public String getJsonString() {
		return new JsonBuilder().toJson(this);
	}

	public PieChart setMaxSize(int size) {
		m_size = size;
		return this;
	}

	public static class Item {
		private String m_title;

		private double m_number;

		public double getNumber() {
			return m_number;
		}

		public String getTitle() {
			return m_title;
		}

		public Item setNumber(double number) {
			m_number = number;
			return this;
		}

		public Item setTitle(String title) {
			m_title = title;
			return this;
		}
	}

	public static class ItemCompartor implements Comparator<Item> {

		@Override
		public int compare(Item o1, Item o2) {
			return (int) (o2.getNumber() - o1.getNumber());
		}
	}
}

@@ -280,17 +280,17 @@
 _size = 
-1
+3
 0;%0A%0A%09pub
@@ -393,19 +393,17 @@
 tle() %7B%0A
-   
+%09
 %09return 
@@ -415,11 +415,9 @@
 le;%0A
-   
+%09
 %7D%0A%0A%09

modify the pie chart default size