You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GooglePieChart.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.charting;
  17. import java.text.MessageFormat;
  18. import java.util.Collections;
  19. import com.gitblit.utils.StringUtils;
  20. /**
  21. * Builds an interactive pie chart using the Visualization API.
  22. *
  23. * @author James Moger
  24. *
  25. */
  26. public class GooglePieChart extends GoogleChart {
  27. private static final long serialVersionUID = 1L;
  28. public GooglePieChart(String tagId, String title, String keyName, String valueName) {
  29. super(tagId, title, keyName, valueName);
  30. }
  31. @Override
  32. protected void appendChart(StringBuilder sb) {
  33. // create dataset
  34. String dName = "data_" + dataName;
  35. line(sb, MessageFormat.format("var {0} = new google.visualization.DataTable();", dName));
  36. line(sb, MessageFormat.format("{0}.addColumn(''string'', ''{1}'');", dName, keyName));
  37. line(sb, MessageFormat.format("{0}.addColumn(''number'', ''{1}'');", dName, valueName));
  38. line(sb, MessageFormat.format("{0}.addRows({1,number,0});", dName, values.size()));
  39. Collections.sort(values);
  40. StringBuilder colors = new StringBuilder("colors:[");
  41. for (int i = 0; i < values.size(); i++) {
  42. ChartValue value = values.get(i);
  43. colors.append('\'');
  44. colors.append(StringUtils.getColor(value.name));
  45. colors.append('\'');
  46. if (i < values.size() - 1) {
  47. colors.append(',');
  48. }
  49. line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 0, ''{2}'');", dName, i,
  50. value.name));
  51. line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 1, {2,number,0.0});", dName,
  52. i, value.value));
  53. }
  54. colors.append(']');
  55. // instantiate chart
  56. String cName = "chart_" + dataName;
  57. line(sb, MessageFormat.format(
  58. "var {0} = new google.visualization.PieChart(document.getElementById(''{1}''));",
  59. cName, tagId));
  60. line(sb,
  61. MessageFormat
  62. .format("{0}.draw({1}, '{' title: ''{4}'', {5}, legend: '{' position:''{6}'' '}' '}');",
  63. cName, dName, width, height, title, colors.toString(), showLegend ? "right" : "none"));
  64. line(sb, "");
  65. }
  66. }