title.js 5.7 KB
Newer Older
L
lang 已提交
1
define(function(require) {
L
Update  
lang 已提交
2

L
lang 已提交
3
    'use strict';
L
Update  
lang 已提交
4 5

    var echarts = require('../echarts');
L
lang 已提交
6 7
    var graphic = require('../util/graphic');
    var layout = require('../util/layout');
L
Update  
lang 已提交
8 9 10 11 12 13 14

    // Model
    echarts.extendComponentModel({

        type: 'title',

        defaultOption: {
L
lang 已提交
15 16 17 18
            // 一级层叠
            zlevel: 0,
            // 二级层叠
            z: 6,
L
Update  
lang 已提交
19
            show: true,
L
lang 已提交
20

L
Update  
lang 已提交
21
            text: '',
L
lang 已提交
22 23 24 25
            // 超链接跳转
            // link: null,
            // 仅支持self | blank
            // target: null,
L
Update  
lang 已提交
26
            subtext: '',
L
lang 已提交
27 28 29 30 31 32 33 34

            // 超链接跳转
            // sublink: null,
            // 仅支持self | blank
            // subtarget: null,

            // 'center' ¦ 'left' ¦ 'right'
            // ¦ {number}(x坐标,单位px)
35
            left: 'left',
L
lang 已提交
36 37
            // 'top' ¦ 'bottom' ¦ 'center'
            // ¦ {number}(y坐标,单位px)
38
            top: 'top',
L
lang 已提交
39 40 41 42 43 44

            // 水平对齐
            // 'auto' | 'left' | 'right'
            // 默认根据 x 的位置判断是左对齐还是右对齐
            //textAlign: null

L
Update  
lang 已提交
45
            backgroundColor: 'rgba(0,0,0,0)',
L
lang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58

            // 标题边框颜色
            borderColor: '#ccc',

            // 标题边框线宽,单位px,默认为0(无边框)
            borderWidth: 0,

            // 标题内边距,单位px,默认各方向内边距为5,
            // 接受数组分别设定上右下左边距,同css
            padding: 5,

            // 主副标题纵向间隔,单位px,默认为10,
            itemGap: 10,
L
Update  
lang 已提交
59 60 61
            textStyle: {
                fontSize: 18,
                fontWeight: 'bolder',
L
lang 已提交
62 63
                // 主标题文字颜色
                color: '#333'
L
Update  
lang 已提交
64 65
            },
            subtextStyle: {
L
lang 已提交
66 67
                // 副标题文字颜色
                color: '#aaa'
L
Update  
lang 已提交
68 69 70 71 72 73 74 75 76 77
            }
        }
    });

    // View
    echarts.extendComponentView({

        type: 'title',

        render: function (titleModel, ecModel, api) {
L
lang 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            this.group.removeAll();

            if (!titleModel.get('show')) {
                return;
            }

            var group = this.group;

            var textStyleModel = titleModel.getModel('textStyle');
            var subtextStyleModel = titleModel.getModel('subtextStyle');

            var textAlign = titleModel.get('textAlign');

            var textEl = new graphic.Text({
                style: {
                    text: titleModel.get('text'),
                    textFont: textStyleModel.getFont(),
L
lang 已提交
95
                    fill: textStyleModel.getTextColor(),
L
lang 已提交
96 97 98 99 100 101
                    textBaseline: 'top'
                }
            });

            var textRect = textEl.getBoundingRect();

P
pah100 已提交
102
            var subText = titleModel.get('subtext');
L
lang 已提交
103 104
            var subTextEl = new graphic.Text({
                style: {
P
pah100 已提交
105
                    text: subText,
L
lang 已提交
106
                    textFont: subtextStyleModel.getFont(),
L
lang 已提交
107
                    fill: subtextStyleModel.getTextColor(),
L
lang 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
                    y: textRect.height + titleModel.get('itemGap'),
                    textBaseline: 'top'
                }
            });

            var link = titleModel.get('link');
            var sublink = titleModel.get('sublink');

            textEl.silent = !link;
            subTextEl.silent = !sublink;

            if (link) {
                textEl.on('click', function () {
                    window.open(link, titleModel.get('target'));
                });
            }
            if (sublink) {
                subTextEl.on('click', function () {
                    window.open(link, titleModel.get('subtarget'));
                });
            }

            group.add(textEl);
P
pah100 已提交
131 132
            subText && group.add(subTextEl);
            // If no subText, but add subTextEl, there will be an empty line.
L
lang 已提交
133

L
lang 已提交
134
            var groupRect = group.getBoundingRect();
L
tweak  
lang 已提交
135
            var layoutOption = titleModel.getBoxLayoutParams();
136 137 138 139
            layoutOption.width = groupRect.width;
            layoutOption.height = groupRect.height;
            var layoutRect = layout.getLayoutRect(
                layoutOption, {
L
lang 已提交
140 141
                    width: api.getWidth(),
                    height: api.getHeight()
L
lang 已提交
142
                }, titleModel.get('padding')
L
lang 已提交
143 144 145
            );
            // Adjust text align based on position
            if (!textAlign) {
146 147
                var p = layoutRect.x / api.getWidth();
                var p2 = (layoutRect.x + layoutRect.width) / api.getWidth();
L
lang 已提交
148

L
lang 已提交
149
                if (p < 0.2) {
L
lang 已提交
150 151
                    textAlign = 'left';
                }
L
lang 已提交
152
                else if (p2 > 0.8) {
153
                    layoutRect.x += layoutRect.width;
L
lang 已提交
154 155
                    textAlign = 'right';
                }
L
lang 已提交
156
                else {
157
                    layoutRect.x += layoutRect.width / 2;
L
lang 已提交
158 159
                    textAlign = 'center';
                }
L
lang 已提交
160
            }
161
            group.position = [layoutRect.x, layoutRect.y];
L
lang 已提交
162
            textEl.style.textAlign = subTextEl.style.textAlign = textAlign;
163 164 165 166
            textEl.dirty();
            subTextEl.dirty();

            // Render background
167
            var padding = layoutRect.margin;
168 169
            var rect = new graphic.Rect({
                shape: {
P
pah100 已提交
170 171
                    x: -padding[3],
                    y: -padding[0],
172 173
                    width: layoutRect.width + padding[1] + padding[3],
                    height: layoutRect.height + padding[0] + padding[2]
174 175 176 177 178 179
                },
                style: {
                    stroke: titleModel.get('borderColor'),
                    fill: titleModel.get('backgroundColor'),
                    lineWidth: titleModel.get('borderWidth')
                },
L
lang 已提交
180
                silent: true
181 182 183 184
            });
            graphic.subPixelOptimizeRect(rect);

            group.add(rect);
L
Update  
lang 已提交
185 186
        }
    });
L
lang 已提交
187
});