コード例 #1
0
  @Override
  public Object export(ChartDataModel model) throws IOException {
    JSONObject obj = new JSONObject();
    JSONObject data = new JSONObject();

    for (Iterator it = model.getData().entrySet().iterator(); it.hasNext(); ) {
      Map.Entry entry = (Map.Entry) it.next();
      ChartRendererBase.addAttribute(data, entry.getKey().toString(), entry.getValue());
    }
    ChartRendererBase.addAttribute(obj, "data", data);

    JSONObject bars = new JSONObject();
    ChartRendererBase.addAttribute(bars, "show", true);
    ChartRendererBase.addAttribute(obj, "bars", bars);

    // label
    ChartRendererBase.addAttribute(obj, "label", model.getAttributes().get("label"));
    // color
    ChartRendererBase.addAttribute(obj, "color", model.getAttributes().get("color"));

    return obj;
  }
コード例 #2
0
    @Override
    public VisitResult visit(VisitContext context, UIComponent target) {

      if (target instanceof AbstractPoint) {

        AbstractPoint p = (AbstractPoint) target;

        Object x = p.getX();
        Object y = p.getY();

        // the first point determine type of data model
        if (model == null) {
          if (x instanceof Number && y instanceof Number) {
            model = new NumberChartDataModel(type);
          } else if (x instanceof String && y instanceof Number) {
            model = new StringChartDataModel(type);
          } else {
            throw new IllegalArgumentException("Not supported type");
          }
        }

        if (model.getKeyType().isAssignableFrom(x.getClass())
            && model.getValueType().isAssignableFrom(y.getClass())) {

          if (x instanceof Number && y instanceof Number) {
            model.put(x, y);
          } else if (x instanceof String && y instanceof Number) {
            model.put(x, y);
          } else {
            throw new IllegalArgumentException(
                "Not supported types "
                    + x.getClass()
                    + " "
                    + y.getClass()
                    + " for "
                    + model.getClass());
          }

        } else {
          throw new IllegalArgumentException(
              "Not supported types "
                  + x.getClass()
                  + " "
                  + y.getClass()
                  + " for "
                  + model.getClass());
        }
      }
      return VisitResult.ACCEPT;
    }
コード例 #3
0
    @Override
    public VisitResult visit(VisitContext context, UIComponent target) {

      if (target instanceof AbstractLegend) {
        copyAttrs(target, chart, "", asList("position", "sorting"));
      } else if (target instanceof AbstractSeries) {
        AbstractSeries s = (AbstractSeries) target;
        ChartDataModel model = s.getData();
        particularSeriesListeners.add(s.getPlotClickListener());

        // Collect Series specific handlers
        Map<String, Object> optMap = new HashMap<String, Object>();
        RenderKitUtils.Attributes seriesEvents =
            attributes()
                .generic("onplothover", "onplothover", "plothover")
                .generic("onplotclick", "onplotclick", "plotclick");

        addToScriptHash(
            optMap,
            context.getFacesContext(),
            target,
            seriesEvents,
            RenderKitUtils.ScriptHashVariableWrapper.eventHandler);

        if (optMap.get("onplotclick") != null) {
          plotClickHandlers.put(new RawJSONString(optMap.get("onplotclick").toString()));
        } else {
          plotClickHandlers.put(s.getOnplotclick());
        }

        if (optMap.get("onplothover") != null) {
          plothoverHandlers.put(new RawJSONString(optMap.get("onplothover").toString()));
        } else {
          plothoverHandlers.put(s.getOnplothover());
        }
        // end collect series specific handler

        if (model == null) {
          /**
           * data model priority: if there is data model passed through data attribute use it.
           * Otherwise nested point tags are expected.
           */
          VisitSeries seriesCallback = new VisitSeries(s.getType());
          s.visitTree(
              VisitContext.createVisitContext(FacesContext.getCurrentInstance()), seriesCallback);
          model = seriesCallback.getModel();

          // if series has no data create empty model
          if (model == null) {
            switch (s.getType()) {
              case line:
                model = new NumberChartDataModel(ChartType.line);
                break;
              case bar:
                model = new NumberChartDataModel(ChartType.bar);
                break;
              case pie:
                model = new StringChartDataModel(ChartType.pie);
                break;
              default:
                break;
            }
          } else {
            nodata = false;
          }
        } else {
          nodata = false;
        }
        model.setAttributes(s.getAttributes());

        try {
          // Check model/series compatibility

          if (chartType == null && (!nodata)) {
            // if series is empty do not set types
            chartType = model.getType();
            keyType = model.getKeyType();
            valType = model.getValueType();
          } else {
            if (chartType == ChartDataModel.ChartType.pie) {
              throw new IllegalArgumentException("Pie chart supports only one series.");
            }
          }
          if (keyType != model.getKeyType() || valType != model.getValueType()) {
            throw new IllegalArgumentException("Data model is not valid for this chart type.");
          }

          data.put(model.export());
        } catch (IOException ex) {
          throw new FacesException(ex);
        }

      } else if (target instanceof AbstractXAxis) {
        copyAttrs(target, chart, "x", asList("min", "max", "pad", "label", "format"));
      } else if (target instanceof AbstractYAxis) {
        copyAttrs(target, chart, "y", asList("min", "max", "pad", "label", "format"));
      }
      return VisitResult.ACCEPT;
    }