2 Star 2 Fork 0

duanyashu / chartscp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

echarts折线图数据生成的搭档chartscp

这个工具可以通过指定参数生成带日期时间的图表,柱状图的初始数据,配合sql实现展示数据 在这里插入图片描述 maven仓库坐标

<dependency>
    <groupId>com.github.duanyashu</groupId>
    <artifactId>chartscp</artifactId>
    <version>1.1.3</version>
</dependency>

使用格式

ChartscpResult build = ChartscpUtils.newBuilder(ChartscpUtils.HOUR).setStartTime("2021-01-18 00:00:00").setEndTime("2021-01-18 23:59:59").setLength(7).setInterval(8).setDataNonzero(true).setXCellFormat("HH时").build();

参数说明:
	newBuilder(ChartscpUtils.HOUR)是指定显示x轴格式,年(YEAR),月(MONTH),日(DATE),小时(HOUR),分钟(MINUTE),周(WEEK),整周(DAY_WHOLE_WEEK),整小时(MINUTE_WHOLE_HOUR),整天(HOUR_WHOLE_DAY),整月(DAY_WHOLE_MONTH),整年(MONTH_WHOLE_YEAR),季度(QUARTER), 必填

	setLength() 这个方法用来指定显示的长度 默认是7 
		eg: Builder(ChartscpUtils.YEAR).setLength(3) 是显示最近3年 xCells=[2019,2020,2021] )

	setStartTime() 和 setEndTime() 是指定日期范围,  指定了日期范围 setLength 失效

	setInterval() 是指定日期步长 默认是1,如果设置为 0 代表日期不需要连续,按数据库查询显示。		
		(eg:Builder(Calendar.YEAR).setLength(3).setInterval(2) 显示最近6年每两年作为一个单位 xCells=[2017,2019,2021])
		
	setDataNonzero(true) 这个方法用来设置 当数据为0,是否显示前一个值(eg: 显示价格变化) 默认是 false
    
	setXCellFormat("HH时")  这个方法用来自定义显示的xCell日期格式 不设置显示默认格式
	
	build() 方法生成结果对象ChartscpResult 如果需要多个datas,可以通过自定义继承ChartscpResult,在build(扩展类)中传入实现。

		eg:扩展类
			public class Kz extends ChartscpResult {
				private List<Integer> datas1;
				public List<Integer> getDatas1() {
					return datas1;
				}
				public void setDatas1(List<Integer> datas1) {
					this.datas1 = datas1;
				}
			}
		引入使用
		 Kz kz = ChartscpUtils.newBuilder(Calendar.DATE).build(Kz.class);

常用示例:

    eg : 默认查询7条数据 通过setLength指定   默认数据间隔1 通过setInterval指定

     * 年为单位 最近3年
     */
    ChartscpResult yearResult = ChartscpUtils.newBuilder(ChartscpUtils.YEAR).setLength(3).build();

在这里插入图片描述 在这里插入图片描述

    /**
     * 月为单位  最近5月
     */
    ChartscpResult monthResult = ChartscpUtils.newBuilder(ChartscpUtils.MONTH).setXCellFormat("yy-M").setLength(5).build();
    /**
     * 日为单位 最近7日
     */
    ChartscpResult dateResult = ChartscpUtils.newBuilder(ChartscpUtils.DATE).setLength(7).build();
    /**
     * 小时为单位 最近7小时
     */
    ChartscpResult hourResult = ChartscpUtils.newBuilder(ChartscpUtils.HOUR).build();
    /**
     * 分钟为单位 最近7分钟
     */
    ChartscpResult minuteResult = ChartscpUtils.newBuilder(ChartscpUtils.MINUTE).build();

    /**
     * 日为单位 间隔2日统计 3个日单位 (eg: xells={1 3 5}))
     */
    ChartscpResult dateIntervalResult = ChartscpUtils.newBuilder(ChartscpUtils.DATE).setLength(3).setInterval(2).build();
    /**
     * 最近7天非连续数据  间隔为0  只生成开始结束日期,数据直接获取数据库
     */
    ChartscpResult dateInterval0Result = ChartscpUtils.newBuilder(Calendar.DATE).setInterval(0).build();
    List<ChartscpResultMap> list1 = new ArrayList<>();
    ChartscpResultMap chartscpResultMap = new ChartscpResultMap();
    chartscpResultMap.setXcell("01-18");
    chartscpResultMap.setData(1);
    list1.add(chartscpResultMap);
    ChartscpResultMap chartscpResultMap1 = new ChartscpResultMap();
    chartscpResultMap1.setXcell("01-23");
    chartscpResultMap1.setData(4);
    list1.add(chartscpResultMap1);
    dateInterval0Result.updateData(list1);

    /**
     * 加开始 ,结束时间  指定日期的数据
     */
    ChartscpResult seResult = ChartscpUtils.newBuilder(ChartscpUtils.HOUR).setStartTime("2021-01-18 00:00:00").setEndTime("2021-01-18 23:59:59").build();

    /**
     * 自定义 显示格式
     */
    ChartscpResult customXCellFormat = ChartscpUtils.newBuilder(ChartscpUtils.HOUR).setXCellFormat("H点m分").setLength(3).build();

    /**
     * 自定义扩展类实现字段扩展
     */
    Kz kz = ChartscpUtils.newBuilder(ChartscpUtils.DATE).build(Kz.class);
    //模拟数据库数据 ChartscpResultMapKz和Kz类字段对应  datas1
    List<ChartscpResultMapKz> list = new ArrayList<>();
    ChartscpResultMapKz sqKz = new ChartscpResultMapKz();
    sqKz.setXcell("01-22");
    sqKz.setData(1);
    sqKz.setDatas1(3);
    list.add(sqKz);
    ChartscpResultMapKz sqKz1 = new ChartscpResultMapKz();
    sqKz1.setXcell("01-24");
    sqKz1.setData(4);
    sqKz1.setDatas1(8);
    list.add(sqKz1);
    //更新数据
    kz.updateData(list);

    /**
     * 按分钟显示整小时
     */
    ChartscpResult minute_whole_hour = ChartscpUtils.newBuilder(MINUTE_WHOLE_HOUR).setDataNonzero(true).setInterval(8).build();
    /**
     * 按分钟显示整小时 ,当前分钟没有数据保持前一分钟数据
     */
    ChartscpResult minute_whole_hour1 = ChartscpUtils.newBuilder(MINUTE_WHOLE_HOUR).setDataNonzero(true).setEndTime("2021-01-25 08:09:23").build();
    //模拟数据库数据 ChartscpResultMapKz和Kz类字段对应  datas1
    List<ChartscpResultMap> list3 = new ArrayList<>();
    ChartscpResultMap crm1 = new ChartscpResultMap();
    crm1.setXcell("11:12");
    crm1.setData(1);
    list3.add(crm1);
    //更新数据
    minute_whole_hour1.updateData(list3);
    /**
     * 按小时显示整天  三班倒
     */
    ChartscpResult hour_whole_day = ChartscpUtils.newBuilder(ChartscpUtils.HOUR_WHOLE_DAY).setInterval(8).setDataNonzero(true).build();

    /**
     * 按天显示整月
     */
    ChartscpResult day_whole_month = ChartscpUtils.newBuilder(ChartscpUtils.DAY_WHOLE_MONTH).build();
    /**
     * 按月显示整年
     */
    ChartscpResult month_whole_year = ChartscpUtils.newBuilder(ChartscpUtils.MONTH_WHOLE_YEAR).build();

    /**
     * 显示当前周数据  如果有多个分类数据,可以通过扩展ChartscpResult和ChartscpResultMap实现
     */
    ChartscpResult week = ChartscpUtils.newBuilder(ChartscpUtils.WEEK).setXCellFormat("周%s").setDataNonzero(true).build();
        //模拟数据库数据 ChartscpResultMapKz和Kz类字段对应  datas1
    List<ChartscpResultMap> list2 = new ArrayList<>();
    ChartscpResultMap crm = new ChartscpResultMap();
    crm.setXcell("01-27");
    crm.setData(1);
    list2.add(crm);
    //更新数据
    week.updateData(list2);

在这里插入图片描述

	/**
     * 显示以周为单位的数据 默认显示最近4周
     */
    ChartscpResult week = ChartscpUtils.newBuilder(ChartscpUtils.WEEK).build();

在这里插入图片描述

	 /**
     * 显示季度数据
     */
    ChartscpResult chartscpResult = ChartscpUtils.newBuilder(ChartscpUtils.QUARTER).setXCellFormat("第%s个季度").setLength(2).build();
    //模拟数据库数据 ChartscpResultMapKz和Kz类字段对应  datas1
    List<ChartscpResultMap> list4 = new ArrayList<>();
    ChartscpResultMap crm11 = new ChartscpResultMap();
    crm11.setXcell("01");
    crm11.setData(4);
    list4.add(crm11);
    ChartscpResultMap crm111 = new ChartscpResultMap();
    crm111.setXcell("03");
    crm111.setData(2);
    list4.add(crm111);
    chartscpResult.updateData(list4);

在这里插入图片描述 针对下边这种带分类的统计可以通过带条件分类(广告收入等)进行多次查询 然后合并数据方式 在这里插入图片描述

public Map<String, Object> quarter() {
        List<String> legend = Arrays.asList("上季度","本季度");
        List<Object> data1 =  new ArrayList<>();
        List<Object> data2 =  new ArrayList<>();
        List<String> yCell = new ArrayList<>();
        List<String> sourceList = Arrays.asList("广告收入,房扭扭收入,置顶收入,明星楼盘");
        for (String source : sourceList) {
            yCell.add(source);
            ChartscpResult resultVo = ChartscpUtils.newBuilder(ChartscpUtils.QUARTER).setLength(2).build();
            //设置条件
            resultVo.setWhere(source);
            //执行查询sql
            List<ChartscpResultMap> chartscpResultMaps = xxxMapper.select(resultVo);
            resultVo.updateData(chartscpResultMaps);
            List datas = resultVo.getDatas();
            data1.add(datas.get(0));
            data2.add(datas.get(1));
        }

        Map<String, Object> map  = new HashMap<>();
        map.put("legend",legend);
        map.put("yCell",yCell);
        map.put("data1",data1);
        map.put("data2",data2);
        return map;
    }

sql语句

 <select id="select" parameterType="com.github.duanyashu.chartscp.ChartscpResult" resultType="com.github.duanyashu.chartscp.ChartscpResultMap">
     select DATE_FORMAT(create_time,#{resultDateFormat}) as xcell ,sum('字段') as data FROM 表名
    where create_time BETWEEN #{startTime} and #{endTime} 
    <if test="where!=null and where!=''">
        and souce=#{where}
    </if>
  GROUP BY DATE_FORMAT(create_time,#{groupByDateFormat}) ORDER BY create_time
</select>

通过上述方法返回一个ChartscpResult类,这个类中有以下属性

  /**
     * x轴数据
     */
    private List<T> xCells;

    /**
     * 数据
     */
    private List<T> datas;

    private Date startTime;

    private Date endTime;

    /**
     * sql结果集日期格式
     */
    private String  resultDateFormat;

    /**
     * sql查询分组日期格式
     */
    private String  groupByDateFormat;

    /**
     * 数据间隔
     */
    private int  interval;

    /**
     * sql业务条件
     */
    private T where;   

data 数据集合,如果需要多个可以通过扩展实现,where 属性是预留业务查询的条件字段,可以将自己的条件携带到sql中来获取。

通过上述方法生成的数据是初始值0,之后需要自己通过sql查询数据,进行替换

###sql示例

mapper.java

List<EchartsMap> selectUserByOneWeek(ChartscpResult chartscpResult);

mapper.xml

sql语句除了数据有间隔使用第二个sql,其他情况都使用以下sql

   <select id="selectUserByOneWeek" resultType="util.ChartscpMap">    
        select DATE_FORMAT(create_time,#{resultDateFormat}) as xcell ,count(*) as data FROM user
        where create_time BETWEEN #{startTime} and #{endTime}
      GROUP BY DATE_FORMAT(create_time,#{groupByDateFormat}) ORDER BY create_time
  </select>
以指定间隔分钟(eg:10分钟)为单位的统计数据sql示例
 <select id="selectUserByOneWeek" resultType="util.ChartscpMap">    
            SELECT DATE_FORMAT(time,#{resultDateFormat}) as xcell ,count(*) as data
            FROM
            	(SELECT  DATE_FORMAT( concat( date( create_time), ' ', HOUR ( create_time), ':', CEIL( MINUTE ( create_time) / #{interval} ) * #{interval} ), #{groupByDateFormat}  ) AS time 
            	FROM user) a 
            GROUP BY time
      </select>

返回结果集是ChartscpMap对象, 这个对象中有两个参数 xcell 对应日期,data对应数据 如果有多个data可以通过扩展ChartscpMap实现

 public class ChartscpMapKz extends ChartscpMap {
 
     private Integer datas1;
 }

最后更新数据

ChartscpResult中有一个updateData方法可以实现数据更新

最终使用方式

public ChartscpResult getUserByOneWeek() {
        ChartscpResult chartscpResult = ChartscpUtils.newBuilder().setCalendarField(Calendar.DATE).build();
        chartscpResult.updateData(userMapper.selectUserByOneWeek(chartscpResult));
        return chartscpResult;
    }
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

图表折线图的好伴侣,快速生成数据,解决折线图数据问题 展开 收起
Java
Apache-2.0
取消

发行版 (3)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/duanyashu/chartscp.git
git@gitee.com:duanyashu/chartscp.git
duanyashu
chartscp
chartscp
main

搜索帮助