AreaChart extends LineChart ,因此工作方式都是相同的。不同之处是替换了单独的丝线,变成了线性区域。
AreaChart germanyChartConfig = new AreaChart(); germanyChartConfig.addValues(68376,72815,78169,78289,79433,82075); germanyChartConfig.setColour("#ff0000"); germanyChartConfig.setText("Germany"); AreaChart franceChartConfig = new AreaChart(); franceChartConfig.addValues(41832,45674,50771,53950,56842,59128); franceChartConfig.setColour("#000066"); franceChartConfig.setText("France");
比如,我们有如下数据
ScatterChart chartConfig = new ScatterChart(); chartConfig.addPoint(41832, 68376); chartConfig.addPoint(45674, 72815); chartConfig.addPoint(50771, 78169); chartConfig.addPoint(53950, 78289); chartConfig.addPoint(56842, 79433); chartConfig.addPoint(59128, 82075);数据设置进去了,但是我们没有办法在图表上设置数据的渲染方式——只有在鼠标经过的他们的时候,才可以显示出来。
在我们RSSReader项目里面,还没有显示任何一个Chart功能。虽然一个小小的demo项目里面还没有什么功能需要用到chart,但是如果往里面加还是能加进去的
让我们新建一个chart显示一周内每天的feed信息的发布时间的统计情况。
@Override protected void onRender(Element parent, int index) { super.onRender(parent, index); setLayout(new FitLayout()); chart.setBorders(true); chart.setVisible(false);//为了避免报错,所以一开始设置不可见,等有chartModel的时候再设置可见 add(chart); } private HashMap<String, Integer> prepareData(List<Item> items) { HashMap<String, Integer> days = new HashMap<String, Integer>(); for (Item item : items) { DateTimeFormat fmt = DateTimeFormat.getFormat("EEEE"); String day = fmt.format(item.getPubDate()); Integer dayOccurance = days.get(day); if (dayOccurance == null) { days.put(day, 1); } else { days.put(day, ++dayOccurance); } } return days; }
private ChartModel createChartModelData(List<Item> items) { // create chartModel ChartModel chartModel = new ChartModel("Posts per week of day", "font-size: 14px; font-family: Verdana; text-align: center;"); chartModel.setBackgroundColour("#ffffff"); // create PieChart PieChart pie = new PieChart(); pie.setColours("#FF0000", "#FFA500", "#FFFF00", "#008000", "#0000FF", "#4B0082", "#EE82EE"); // add Slice, set values HashMap<String, Integer> days = prepareData(items); for (String key : days.keySet()) { pie.addSlices(new PieChart.Slice(days.get(key), key)); } chartModel.addChartConfig(pie); return chartModel; }
public void setFeed(final Feed feed) { final FeedServiceAsync feedService = Registry .get(RSSReaderConstants.FEED_SERVICE); //根据uuid获得其items feedService.loadItems(feed.getUuid(), new AsyncCallback<List<Item>>() { @Override public void onFailure(Throwable caught) { Dispatcher.forwardEvent(AppEvents.Error, caught); } @Override public void onSuccess(List<Item> items) { //成功之后将items转换成ChartModel,设置到chart里。 chart.setChartModel(createChartModelData(items)); } }); }
public void setFeed(final Feed feed) { final FeedServiceAsync feedService = Registry .get(RSSReaderConstants.FEED_SERVICE); // 根据uuid获得其items feedService.loadItems(feed.getUuid(), new AsyncCallback<List<Item>>() { @Override public void onFailure(Throwable caught) { Dispatcher.forwardEvent(AppEvents.Error, caught); } @Override public void onSuccess(List<Item> items) { // 成功之后将items转换成ChartModel,设置到chart里。 chart.setChartModel(createChartModelData(items)); } }); // if (!chart.isVisible()) { chart.setVisible(true); } }
private void onFeedsDropped(DNDEvent event) { // 接收 feeds,将feed传入到feedChart中去。 List<BeanModel> beanModels = event.getData(); for (BeanModel beanModel : beanModels) { Feed feed = beanModel.getBean(); feedChart.setFeed(feed); } }
package com.danielvaughan.rssreader.client.portlets; import java.util.List; import com.danielvaughan.rssreader.client.RSSReaderConstants; import com.danielvaughan.rssreader.client.charts.FeedChart; import com.danielvaughan.rssreader.client.mvc.events.AppEvents; import com.danielvaughan.rssreader.shared.model.Feed; import com.extjs.gxt.ui.client.dnd.DND; import com.extjs.gxt.ui.client.dnd.DropTarget; import com.extjs.gxt.ui.client.event.DNDEvent; import com.extjs.gxt.ui.client.mvc.Dispatcher; import com.extjs.gxt.ui.client.widget.custom.Portlet; import com.extjs.gxt.ui.client.widget.layout.FitLayout; import com.google.gwt.user.client.Element; public class ChartPortlet extends Portlet { private final FeedChart feedChart = new FeedChart(); public ChartPortlet() { setHeading("Chart"); setId(RSSReaderConstants.CHART_PORTLET); setLayout(new FitLayout()); setHeight(250); Dispatcher.forwardEvent(AppEvents.NewPortletCreated, this); } private void onFeedsDropped(DNDEvent event) { // 接收 feeds,将feed传入到feedChart中去。 List<BeanModel> beanModels = event.getData(); for (BeanModel beanModel : beanModels) { Feed feed = beanModel.getBean(); feedChart.setFeed(feed); } } @Override protected void onRender(Element parent, int index) { super.onRender(parent, index); DropTarget target = new DropTarget(this) { @Override protected void onDragDrop(DNDEvent event) { super.onDragDrop(event); onFeedsDropped(event); } }; target.setOperation(DND.Operation.COPY); target.setGroup(RSSReaderConstants.FEED_DD_GROUP); } }
package com.danielvaughan.rssreader.client.charts; import java.util.HashMap; import java.util.List; import com.danielvaughan.rssreader.client.RSSReaderConstants; import com.danielvaughan.rssreader.client.mvc.events.AppEvents; import com.danielvaughan.rssreader.client.services.FeedServiceAsync; import com.danielvaughan.rssreader.shared.model.Feed; import com.danielvaughan.rssreader.shared.model.Item; import com.extjs.gxt.charts.client.Chart; import com.extjs.gxt.charts.client.model.ChartModel; import com.extjs.gxt.charts.client.model.charts.PieChart; import com.extjs.gxt.ui.client.Registry; import com.extjs.gxt.ui.client.mvc.Dispatcher; import com.extjs.gxt.ui.client.widget.LayoutContainer; import com.extjs.gxt.ui.client.widget.layout.FitLayout; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.rpc.AsyncCallback; public class FeedChart extends LayoutContainer { private final Chart chart = new Chart("gxt/chart/open-flash-chart.swf"); private ChartModel createChartModelData(List<Item> items) { ChartModel chartModel = new ChartModel("Posts per week of day", "font-size: 14px; font-family: Verdana; text-align: center;"); chartModel.setBackgroundColour("#ffffff"); PieChart pie = new PieChart(); pie.setColours("#FF0000", "#FFA500", "#FFFF00", "#008000", "#0000FF", "#4B0082", "#EE82EE"); HashMap<String, Integer> days = prepareData(items); for (String key : days.keySet()) { pie.addSlices(new PieChart.Slice(days.get(key), key)); } chartModel.addChartConfig(pie); return chartModel; } @Override protected void onRender(Element parent, int index) { super.onRender(parent, index); setLayout(new FitLayout()); chart.setBorders(true); chart.setVisible(false); add(chart); } private HashMap<String, Integer> prepareData(List<Item> items) { HashMap<String, Integer> days = new HashMap<String, Integer>(); for (Item item : items) { DateTimeFormat fmt = DateTimeFormat.getFormat("EEEE"); String day = fmt.format(item.getPubDate()); Integer dayOccurance = days.get(day); if (dayOccurance == null) { days.put(day, 1); } else { days.put(day, ++dayOccurance); } } return days; } public void setFeed(final Feed feed) { final FeedServiceAsync feedService = Registry .get(RSSReaderConstants.FEED_SERVICE); feedService.loadItems(feed.getUuid(), new AsyncCallback<List<Item>>() { @Override public void onFailure(Throwable caught) { Dispatcher.forwardEvent(AppEvents.Error, caught); } @Override public void onSuccess(List<Item> items) { chart.setChartModel(createChartModelData(items)); } }); if (!chart.isVisible()) { chart.setVisible(true); } } }