| 1 | package opts |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | |
| 7 | "github.com/go-echarts/go-echarts/v2/types" |
| 8 | ) |
| 9 | |
| 10 | // Toolbox is the option set for a toolbox component. |
| 11 | // https://echarts.apache.org/en/option.html#toolbox |
| 12 | type Toolbox struct { |
| 13 | // Whether to show toolbox component. |
| 14 | Show types.Bool `json:"show,omitempty"` |
| 15 | |
| 16 | // The layout orientation of toolbox's icon. |
| 17 | // Options: 'horizontal','vertical' |
| 18 | Orient string `json:"orient,omitempty"` |
| 19 | |
| 20 | // Distance between toolbox component and the left side of the container. |
| 21 | // left value can be instant pixel value like 20; it can also be a percentage |
| 22 | // value relative to container width like '20%'; and it can also be 'left', 'center', or 'right'. |
| 23 | // If the left value is set to be 'left', 'center', or 'right', then the component |
| 24 | // will be aligned automatically based on position. |
| 25 | Left string `json:"left,omitempty"` |
| 26 | |
| 27 | // Distance between toolbox component and the top side of the container. |
| 28 | // top value can be instant pixel value like 20; it can also be a percentage |
| 29 | // value relative to container width like '20%'; and it can also be 'top', 'middle', or 'bottom'. |
| 30 | // If the left value is set to be 'top', 'middle', or 'bottom', then the component |
| 31 | // will be aligned automatically based on position. |
| 32 | Top string `json:"top,omitempty"` |
| 33 | |
| 34 | // Distance between toolbox component and the right side of the container. |
| 35 | // right value can be instant pixel value like 20; it can also be a percentage |
| 36 | // value relative to container width like '20%'. |
| 37 | // Adaptive by default. |
| 38 | Right string `json:"right,omitempty"` |
| 39 | |
| 40 | // Distance between toolbox component and the bottom side of the container. |
| 41 | // bottom value can be instant pixel value like 20; it can also be a percentage |
| 42 | // value relative to container width like '20%'. |
| 43 | // Adaptive by default. |
| 44 | Bottom string `json:"bottom,omitempty"` |
| 45 | |
| 46 | // The configuration item for each tool. |
| 47 | // Besides the tools we provide, user-defined toolbox is also supported. |
| 48 | Feature *ToolBoxFeature `json:"feature,omitempty"` |
| 49 | } |
| 50 | |
| 51 | // ToolBoxFeature is a feature component under toolbox. |
| 52 | // https://echarts.apache.org/en/option.html#toolbox |
| 53 | type ToolBoxFeature struct { |
| 54 | // Save as image tool |
| 55 | SaveAsImage *ToolBoxFeatureSaveAsImage `json:"saveAsImage,omitempty"` |
| 56 | |
| 57 | // Data brush |
| 58 | Brush *ToolBoxFeatureBrush `json:"brush,omitempty"` |
| 59 | |
| 60 | // Data area zooming, which only supports rectangular coordinate by now. |
| 61 | DataZoom *ToolBoxFeatureDataZoom `json:"dataZoom,omitempty"` |
| 62 | |
| 63 | // Data view tool, which could display data in current chart and updates chart after being edited. |
| 64 | DataView *ToolBoxFeatureDataView `json:"dataView,omitempty"` |
| 65 | |
| 66 | // Restore configuration item. |
| 67 | Restore *ToolBoxFeatureRestore `json:"restore,omitempty"` |
| 68 | |
| 69 | // User-defined tools. They have to start with "my". |
| 70 | UserDefined map[string]ToolBoxFeatureUserDefined `json:"-"` |
| 71 | } |
| 72 | |
| 73 | func (f ToolBoxFeature) MarshalJSON() ([]byte, error) { |
| 74 | type ToolBoxFeature_ ToolBoxFeature |
| 75 | |
| 76 | buff := new(bytes.Buffer) |
| 77 | enc := json.NewEncoder(buff) |
| 78 | enc.SetEscapeHTML(false) |
| 79 | |
| 80 | err := enc.Encode(ToolBoxFeature_(f)) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | // Remove trailing newline. |
| 85 | buff.Truncate(buff.Len() - 1) |
| 86 | |
| 87 | if len(f.UserDefined) == 0 { |
| 88 | return buff.Bytes(), nil |
| 89 | } |
| 90 | |
| 91 | user := new(bytes.Buffer) |
| 92 | enc = json.NewEncoder(user) |
| 93 | enc.SetEscapeHTML(false) |
| 94 | |
| 95 | err = enc.Encode(f.UserDefined) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | // Remove trailing newline. |
| 100 | user.Truncate(user.Len() - 1) |
| 101 | |
| 102 | // Remove trailing "}". |
| 103 | buff.Truncate(buff.Len() - 1) |
| 104 | _, _ = buff.WriteString(",") |
| 105 | // Remove prefix "{". |
| 106 | _, _ = user.ReadByte() |
| 107 | // Copy user-defined tools over. |
| 108 | _, _ = buff.ReadFrom(user) |
| 109 | return buff.Bytes(), nil |
| 110 | } |
| 111 | |
| 112 | // ToolBoxFeatureUserDefined is the option fro user-defined tools. |
| 113 | // https://echarts.apache.org/en/option.html#toolbox.feature |
| 114 | type ToolBoxFeatureUserDefined struct { |
| 115 | // Whether to show the tool. |
| 116 | Show types.Bool `json:"show,omitempty"` |
| 117 | |
| 118 | // Title for the tool. |
| 119 | Title string `json:"title,omitempty"` |
| 120 | |
| 121 | // Icon for the tool. |
| 122 | // It can be set to an image with 'image://url' , in which URL is the link to an image, or dataURI of an image. |
| 123 | // Icons can be also set to arbitrary vector path via 'path://', like ''path://M30.9,53.2C16.8,53.2,5.3,41......' |
| 124 | Icon string `json:"icon,omitempty"` |
| 125 | |
| 126 | // On click handler in JavaScript. Use opts.FuncOpts to embed JavaScript. |
| 127 | OnClick types.FuncStr `json:"onclick,omitempty"` |
| 128 | } |
| 129 | |
| 130 | // ToolBoxFeatureSaveAsImage is the option for saving chart as image. |
| 131 | // https://echarts.apache.org/en/option.html#toolbox.feature.saveAsImage |
| 132 | type ToolBoxFeatureSaveAsImage struct { |
| 133 | // Whether to show the tool. |
| 134 | Show types.Bool `json:"show,omitempty"` |
| 135 | |
| 136 | // toolbox.feature.saveAsImage. type = 'png' |
| 137 | // File suffix of the image saved. |
| 138 | // If the renderer is set to be 'canvas' when chart initialized (default), t |
| 139 | // hen 'png' (default) and 'jpeg' are supported. |
| 140 | // If the renderer is set to be 'svg' when when chart initialized, then only 'svg' is supported |
| 141 | // for type ('svg' type is supported since v4.8.0). |
| 142 | Type string `json:"type,omitempty" default:"png"` |
| 143 | |
| 144 | // Name to save the image, whose default value is title.text. |
| 145 | Name string `json:"name,omitempty"` |
| 146 | |
| 147 | // Title for the tool. |
| 148 | Title string `json:"title,omitempty"` |
| 149 | |
| 150 | // Icon for the tool. |
| 151 | // It can be set to an image with 'image://url' , in which URL is the link to an image, or dataURI of an image. |
| 152 | // Icons can be also set to arbitrary vector path via 'path://', like ''path://M30.9,53.2C16.8,53.2,5.3,41......' |
| 153 | Icon string `json:"icon,omitempty"` |
| 154 | } |
| 155 | |
| 156 | // ToolBoxFeatureBrush brush-selecting icon. |
| 157 | // https://echarts.apache.org/en/option.html#toolbox.feature.brush |
| 158 | type ToolBoxFeatureBrush struct { |
| 159 | // Icons used, whose values are: |
| 160 | // 'rect': Enabling selecting with rectangle area. |
| 161 | // 'polygon': Enabling selecting with any shape. |
| 162 | // 'lineX': Enabling horizontal selecting. |
| 163 | // 'lineY': Enabling vertical selecting. |
| 164 | // 'keep': Switching between single selecting and multiple selecting. The latter one can select multiple areas, while the former one cancels previous selection. |
| 165 | // 'clear': Clearing all selection. |
| 166 | Type []string `json:"type,omitempty"` |
| 167 | } |
| 168 | |
| 169 | // ToolBoxFeatureDataZoom |
| 170 | // https://echarts.apache.org/en/option.html#toolbox.feature.dataZoom |
| 171 | type ToolBoxFeatureDataZoom struct { |
| 172 | // Whether to show the tool. |
| 173 | Show types.Bool `json:"show,omitempty"` |
| 174 | |
| 175 | // Defines which yAxis should be controlled. By default, it controls all y axes. |
| 176 | // If it is set to be false, then no y axis is controlled. |
| 177 | // If it is set to be then it controls axis with axisIndex of 3. |
| 178 | // If it is set to be [0, 3], it controls the x-axes with axisIndex of 0 and 3. |
| 179 | YAxisIndex interface{} `json:"yAxisIndex,omitempty"` |
| 180 | |
| 181 | // Icon for the tool. |
| 182 | // It can be set to an image with 'image://url' , in which URL is the link to an image, or dataURI of an image. |
| 183 | // Icons can be also set to arbitrary vector path via 'path://', like ''path://M30.9,53.2C16.8,53.2,5.3,41......' |
| 184 | // m["zoom"] = "image://url.." |
| 185 | // m["back"] = "path://M78..." |
| 186 | Icon map[string]string `json:"icon"` |
| 187 | |
| 188 | // Restored and zoomed title text. |
| 189 | // m["zoom"] = "area zooming" |
| 190 | // m["back"] = "restore area zooming" |
| 191 | Title map[string]string `json:"title"` |
| 192 | } |
| 193 | |
| 194 | // ToolBoxFeatureDataView |
| 195 | // https://echarts.apache.org/en/option.html#toolbox.feature.dataView |
| 196 | type ToolBoxFeatureDataView struct { |
| 197 | // Whether to show the tool. |
| 198 | Show types.Bool `json:"show,omitempty"` |
| 199 | |
| 200 | // title for the tool. |
| 201 | Title string `json:"title,omitempty"` |
| 202 | |
| 203 | // Icon for the tool. |
| 204 | // It can be set to an image with 'image://url' , in which URL is the link to an image, or dataURI of an image. |
| 205 | // Icons can be also set to arbitrary vector path via 'path://', like ''path://M30.9,53.2C16.8,53.2,5.3,41......' |
| 206 | Icon string `json:"icon,omitempty"` |
| 207 | |
| 208 | // There are 3 names in data view |
| 209 | // you could set them like this: []string["data view", "turn off", "refresh"] |
| 210 | Lang []string `json:"lang"` |
| 211 | |
| 212 | // Background color of the floating layer in data view. |
| 213 | BackgroundColor string `json:"backgroundColor"` |
| 214 | } |
| 215 | |
| 216 | // ToolBoxFeatureRestore |
| 217 | // https://echarts.apache.org/en/option.html#toolbox.feature.restore |
| 218 | type ToolBoxFeatureRestore struct { |
| 219 | // Whether to show the tool. |
| 220 | Show types.Bool `json:"show,omitempty"` |
| 221 | |
| 222 | // title for the tool. |
| 223 | Title string `json:"title,omitempty"` |
| 224 | |
| 225 | // Icon for the tool. |
| 226 | // It can be set to an image with 'image://url' , in which URL is the link to an image, or dataURI of an image. |
| 227 | // Icons can be also set to arbitrary vector path via 'path://', like ''path://M30.9,53.2C16.8,53.2,5.3,41......' |
| 228 | Icon string `json:"icon,omitempty"` |
| 229 | } |