package com.dianping.cat.report.page;

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

import com.google.gson.Gson;

public class PieChart {

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

	public PieChart() {
	}

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

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

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

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

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

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

	public static class Item {
		private String title;

		private double number;

		public double getNumber() {
			return number;
		}

		public String getTitle() {
			return title;
		}

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

		public Item setTitle(String title) {
			this.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());
		}
	}
}

@@ -256,29 +256,43 @@
 %0A%0A%09p
-ublic PieChart() %7B%0A%09%7D
+rivate transient int MAX_SIZE = 10;
 %0A%0A%09p
@@ -532,28 +532,8 @@
 e();
-%0A%09%09int maxSize = 10;
 %0A%0A%09%09
@@ -544,23 +544,24 @@
 size %3C= 
-maxSize
+MAX_SIZE
 ) %7B%0A%09%09%09t
@@ -613,23 +613,24 @@
  0; i %3C 
-maxSize
+MAX_SIZE
 ; i++) %7B
@@ -754,15 +754,16 @@
 i = 
-maxSize
+MAX_SIZE
 ; i 

modify the piechart