Subversion Repositories SmartDukaan

Rev

Rev 28473 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import com.spice.profitmandi.common.model.*;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Component
public class ChartServiceImpl implements ChartService {

        @Override
        public ChartModel createChart(int monthValue, Map<String, List<Double>> sortedBrandValue, List<String> colorList,
                                                                  List<String> borderList, String title) {

                LocalDateTime curDate = LocalDate.now().atStartOfDay();

                LinkedHashSet<String> labels = new LinkedHashSet<String>();

                for (int i = monthValue; i >= 0; i--) {

                        LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
                        labels.add(startMonth.format(DateTimeFormatter.ofPattern("MMM''uu")));
                }

                return createChartWithLabels(labels, sortedBrandValue, colorList,
                                borderList, title);

        }

        @Override
        public ChartModel createChartWithLabels(LinkedHashSet<String> labels, Map<String, List<Double>> sortedBrandValue, List<String> colorList, List<String> borderList, String title) {

                List<DatasetModel> datasets = new ArrayList<>();

                int i = 0;

                for (Entry<String, List<Double>> brandValue : sortedBrandValue.entrySet()) {

                        DatasetModel lmsModel = new DatasetModel();
                        lmsModel.setLabel(brandValue.getKey());
                        lmsModel.setBackgroundColor(colorList.get(i));

                        lmsModel.setData(brandValue.getValue());

                        lmsModel.setType("line");
                        lmsModel.setOrder(1);
                        lmsModel.setFill("false");
                        lmsModel.setBorderColor(borderList.get(i));
                        datasets.add(lmsModel);

                        i++;
                }

                DataModel dm = new DataModel();
                dm.setDatasets(datasets);
                dm.setLabels(labels);

                LegendModel lm = new LegendModel();
                lm.setPosition("top");

                Tooltips tooltips = new Tooltips();
                tooltips.setBodyFontSize(15);
                tooltips.setTitleFontSize(15);
                tooltips.setMode("index");
                tooltips.setIntersect(false);
                tooltips.setBackgroundColor("rgba(0, 0, 0, .5)");

                HoverModel hover = new HoverModel();
                hover.setIntersect(false);
                hover.setMode("index");

                TitleModel tm = new TitleModel();
                tm.setText(title);
                tm.setDisplay(true);
                tm.setFontSize(20);
                tm.setFontColor("#111");

                OptionsModel om = new OptionsModel();
                om.setResponsive(true);
                om.setLegend(lm);
                om.setTitle(tm);
                om.setTooltips(tooltips);
                om.setHover(hover);

                ChartModel cm = new ChartModel();

                cm.setType("line");
                cm.setData(dm);
                cm.setOptions(om);

                return cm;
        }

}