View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.widgets;
20  
21  import java.util.Map;
22  import org.apache.syncope.client.console.chartjs.ChartJSPanel;
23  import org.apache.syncope.client.console.chartjs.Doughnut;
24  import org.apache.syncope.client.console.chartjs.DoughnutAndPieChartData;
25  import org.apache.wicket.model.Model;
26  
27  public class UsersByStatusWidget extends BaseWidget {
28  
29      private static final long serialVersionUID = -816175678514035085L;
30  
31      private static final String[] COLORS = { "green", "orange", "aqua", "red", "gray" };
32  
33      private Map<String, Integer> usersByStatus;
34  
35      private final ChartJSPanel chart;
36  
37      public UsersByStatusWidget(final String id, final Map<String, Integer> usersByStatus) {
38          super(id);
39          this.usersByStatus = usersByStatus;
40          setOutputMarkupId(true);
41  
42          chart = new ChartJSPanel("chart", Model.of(build(usersByStatus)));
43          add(chart);
44      }
45  
46      private static Doughnut build(final Map<String, Integer> usersByStatus) {
47          Doughnut doughnut = new Doughnut();
48          doughnut.getOptions().setResponsive(true);
49          doughnut.getOptions().setMaintainAspectRatio(true);
50  
51          DoughnutAndPieChartData data = new DoughnutAndPieChartData();
52          doughnut.setData(data);
53  
54          DoughnutAndPieChartData.DataSet dataset = new DoughnutAndPieChartData.DataSet();
55          data.getDatasets().add(dataset);
56  
57          int i = 0;
58          for (Map.Entry<String, Integer> entry : usersByStatus.entrySet()) {
59              dataset.getData().add(entry.getValue());
60              dataset.getBackgroundColor().add(COLORS[i % 5]);
61              data.getLabels().add(entry.getKey());
62  
63              i++;
64          }
65  
66          return doughnut;
67      }
68  
69      public boolean refresh(final Map<String, Integer> usersByStatus) {
70          if (!this.usersByStatus.equals(usersByStatus)) {
71              this.usersByStatus = usersByStatus;
72              chart.setDefaultModelObject(build(usersByStatus));
73              return true;
74          }
75          return false;
76      }
77  }