@@ -604,6 +604,272 @@ window.teaweb = {
604
604
chart . setOption ( option )
605
605
chart . resize ( )
606
606
} ,
607
+ renderGaugeChart : function ( options ) {
608
+ let chartId = options . id
609
+ let name = options . name // 标题
610
+ let min = options . min // 最小值
611
+ let max = options . max // 最大值
612
+ let value = options . value // 当前值
613
+ let unit = options . unit // 单位
614
+ let detail = options . detail // 说明文字
615
+ let color = options . color // 颜色
616
+ let startAngle = options . startAngle
617
+ if ( startAngle == null ) {
618
+ startAngle = 225
619
+ }
620
+ let endAngle = options . endAngle
621
+ if ( endAngle == null ) {
622
+ endAngle = - 45
623
+ }
624
+
625
+ color = this . chartColor ( color )
626
+
627
+ let chartBox = document . getElementById ( chartId )
628
+ if ( chartBox == null ) {
629
+ return
630
+ }
631
+ let chart = this . initChart ( chartBox )
632
+
633
+ let option = {
634
+ textStyle : {
635
+ fontFamily : "Lato,'Helvetica Neue',Arial,Helvetica,sans-serif"
636
+ } ,
637
+ color : color ,
638
+ title : ( name != null && name . length > 0 ) ? {
639
+ text : name ,
640
+ top : 1 ,
641
+ bottom : 0 ,
642
+ x : "center" ,
643
+ textStyle : {
644
+ fontSize : 12 ,
645
+ fontWeight : "bold" ,
646
+ fontFamily : "Lato,'Helvetica Neue',Arial,Helvetica,sans-serif"
647
+ }
648
+ } : null ,
649
+ legend : {
650
+ data : [ "" ]
651
+ } ,
652
+ xAxis : {
653
+ data : [ ]
654
+ } ,
655
+ yAxis : { } ,
656
+ series : [ {
657
+ name : "" ,
658
+ type : "gauge" ,
659
+ min : min ,
660
+ max : max ,
661
+ startAngle : startAngle ,
662
+ endAngle : endAngle ,
663
+
664
+ data : [
665
+ {
666
+ "name" : "" , // 不显示名称
667
+ "value" : Math . round ( value * 100 ) / 100
668
+ }
669
+ ] ,
670
+ radius : "100%" ,
671
+ center : [ "50%" , ( name != null && name . length > 0 ) ? "60%" : "50%" ] ,
672
+
673
+ splitNumber : 5 ,
674
+ splitLine : {
675
+ length : 4
676
+ } ,
677
+
678
+ axisLine : {
679
+ lineStyle : {
680
+ width : 4
681
+ }
682
+ } ,
683
+ axisTick : {
684
+ show : true ,
685
+ length : 2
686
+ } ,
687
+ axisLabel : {
688
+ formatter : function ( v ) {
689
+ return v ;
690
+ } ,
691
+ textStyle : {
692
+ fontSize : 8
693
+ }
694
+ } ,
695
+ progress : {
696
+ show : true ,
697
+ width : 4
698
+ } ,
699
+ detail : {
700
+ formatter : function ( v ) {
701
+ return unit ;
702
+ } ,
703
+ textStyle : {
704
+ fontSize : 12 ,
705
+ fontWeight : "normal" ,
706
+ fontFamily : "Arial,Helvetica,sans-serif" ,
707
+ color : "grey"
708
+ //lineHeight: 16
709
+ } ,
710
+ valueAnimation : true
711
+ } ,
712
+
713
+ pointer : {
714
+ width : 2
715
+ }
716
+ } ] ,
717
+
718
+ grid : {
719
+ left : - 2 ,
720
+ right : 0 ,
721
+ bottom : 0 ,
722
+ top : 0
723
+ } ,
724
+ axisPointer : {
725
+ show : false
726
+ } ,
727
+ tooltip : {
728
+ formatter : 'X:{b0} Y:{c0}' ,
729
+ show : false
730
+ } ,
731
+ animation : true
732
+ } ;
733
+
734
+ chart . setOption ( option )
735
+ } ,
736
+
737
+ renderPercentChart : function ( options ) {
738
+ let chartId = options . id
739
+ let color = this . chartColor ( options . color )
740
+ let value = options . value
741
+ let name = options . name
742
+ let total = options . total
743
+ if ( total == null ) {
744
+ total = 100
745
+ }
746
+ let unit = options . unit
747
+ if ( unit == null ) {
748
+ unit = ""
749
+ }
750
+
751
+ let max = options . max
752
+ if ( max != null && max <= value ) {
753
+ max = null
754
+ }
755
+ let maxColor = this . chartColor ( options . maxColor )
756
+ let maxName = options . maxName
757
+
758
+ let chartBox = document . getElementById ( chartId )
759
+ if ( chartBox == null ) {
760
+ return
761
+ }
762
+ let chart = this . initChart ( chartBox )
763
+
764
+ let option = {
765
+ tooltip : {
766
+ formatter : "{a} <br/>{b} : {c}" + unit
767
+ } ,
768
+ series : [
769
+ {
770
+ name : name ,
771
+ max : total ,
772
+ type : "gauge" ,
773
+ radius : "100%" ,
774
+ detail : {
775
+ formatter : "{value}" ,
776
+ show : false ,
777
+ valueAnimation : true
778
+ } ,
779
+ data : [
780
+ {
781
+ value : value ,
782
+ name : name
783
+ }
784
+ ] ,
785
+ pointer : {
786
+ show : false
787
+ } ,
788
+ splitLine : {
789
+ show : false
790
+ } ,
791
+ axisTick : {
792
+ show : false
793
+ } ,
794
+ axisLine : {
795
+ show : true ,
796
+ lineStyle : {
797
+ width : 4
798
+ }
799
+ } ,
800
+ progress : {
801
+ show : true ,
802
+ width : 4 ,
803
+ itemStyle : {
804
+ color : color
805
+ }
806
+ } ,
807
+ splitNumber : {
808
+ show : false
809
+ } ,
810
+ title : {
811
+ show : false
812
+ } ,
813
+ startAngle : 270 ,
814
+ endAngle : - 90
815
+ }
816
+ ]
817
+ }
818
+
819
+ if ( max != null ) {
820
+ option . series . push ( {
821
+ name : maxName ,
822
+ max : total ,
823
+ type : "gauge" ,
824
+ radius : "100%" ,
825
+ detail : {
826
+ formatter : "{value}" ,
827
+ show : false ,
828
+ valueAnimation : true
829
+ } ,
830
+ data : [
831
+ {
832
+ value : max ,
833
+ name : maxName
834
+ }
835
+ ] ,
836
+ pointer : {
837
+ show : false
838
+ } ,
839
+ splitLine : {
840
+ show : false
841
+ } ,
842
+ axisTick : {
843
+ show : false
844
+ } ,
845
+ axisLine : {
846
+ show : true ,
847
+ lineStyle : {
848
+ width : 4
849
+ }
850
+ } ,
851
+ progress : {
852
+ show : true ,
853
+ width : 4 ,
854
+ itemStyle : {
855
+ color : maxColor ,
856
+ opacity : 0.3
857
+ }
858
+ } ,
859
+ splitNumber : {
860
+ show : false
861
+ } ,
862
+ title : {
863
+ show : false
864
+ } ,
865
+ startAngle : 270 ,
866
+ endAngle : - 90
867
+ } )
868
+ }
869
+
870
+ chart . setOption ( option )
871
+ } ,
872
+
607
873
xRotation : function ( chart , names ) {
608
874
let chartWidth = chart . getWidth ( )
609
875
let width = 0
@@ -616,11 +882,17 @@ window.teaweb = {
616
882
617
883
return [ 40 , - 20 ]
618
884
} ,
885
+ chartMap : { } , // dom id => chart
619
886
initChart : function ( dom ) {
887
+ let domId = dom . getAttribute ( "id" )
888
+ if ( domId != null && domId . length > 0 && typeof ( this . chartMap [ domId ] ) == "object" ) {
889
+ return this . chartMap [ domId ]
890
+ }
620
891
let instance = echarts . init ( dom )
621
892
window . addEventListener ( "resize" , function ( ) {
622
893
instance . resize ( )
623
894
} )
895
+ this . chartMap [ domId ] = instance
624
896
return instance
625
897
} ,
626
898
encodeHTML : function ( s ) {
@@ -629,6 +901,25 @@ window.teaweb = {
629
901
s = s . replace ( / > / g, ">" )
630
902
s = s . replace ( / " / , """ )
631
903
return s
904
+ } ,
905
+ chartColor : function ( color ) {
906
+ if ( color == null || color . length == 0 ) {
907
+ color = "#5470c6"
908
+ }
909
+
910
+ if ( color == "red" ) {
911
+ color = "#ee6666"
912
+ }
913
+ if ( color == "yellow" ) {
914
+ color = "#fac858"
915
+ }
916
+ if ( color == "blue" ) {
917
+ color = "#5470c6"
918
+ }
919
+ if ( color == "green" ) {
920
+ color = "#3ba272"
921
+ }
922
+ return color
632
923
}
633
924
}
634
925
0 commit comments