|
3 | 3 |
|
4 | 4 | from pyecharts import options as opts |
5 | 5 | from pyecharts.charts import BMap, Page |
| 6 | +from pyecharts.commons.utils import JsCode |
6 | 7 | from pyecharts.faker import Collector, Faker |
7 | | -from pyecharts.globals import BMapType |
| 8 | +from pyecharts.globals import BMapType, ChartType |
8 | 9 |
|
9 | 10 | C = Collector() |
10 | 11 | BAIDU_MAP_AK = os.environ.get("BAIDU_MAP_AK", "FAKE_AK") |
@@ -162,4 +163,210 @@ def bmap_lines() -> BMap: |
162 | 163 | return c |
163 | 164 |
|
164 | 165 |
|
| 166 | +@C.funcs |
| 167 | +def bmap_custom() -> BMap: |
| 168 | + with open( |
| 169 | + os.path.join("fixtures", "bmap-custom-data.json"), "r", encoding="utf-8" |
| 170 | + ) as f: |
| 171 | + j = json.load(f) |
| 172 | + color_list = ["#070093", "#1c3fbf", "#1482e5", "#70b4eb", "#b4e0f3", "#ffffff"] |
| 173 | + c = ( |
| 174 | + BMap() |
| 175 | + .add_schema( |
| 176 | + baidu_ak=BAIDU_MAP_AK, |
| 177 | + center=[116.46, 39.92], |
| 178 | + zoom=11.8, |
| 179 | + is_roam=True, |
| 180 | + map_style={ |
| 181 | + "styleJson": [ |
| 182 | + { |
| 183 | + "featureType": "water", |
| 184 | + "elementType": "all", |
| 185 | + "stylers": {"color": "#d1d1d1"}, |
| 186 | + }, |
| 187 | + { |
| 188 | + "featureType": "land", |
| 189 | + "elementType": "all", |
| 190 | + "stylers": {"color": "#f3f3f3"}, |
| 191 | + }, |
| 192 | + { |
| 193 | + "featureType": "railway", |
| 194 | + "elementType": "all", |
| 195 | + "stylers": {"visibility": "off"}, |
| 196 | + }, |
| 197 | + { |
| 198 | + "featureType": "highway", |
| 199 | + "elementType": "all", |
| 200 | + "stylers": {"color": "#999999"}, |
| 201 | + }, |
| 202 | + { |
| 203 | + "featureType": "highway", |
| 204 | + "elementType": "labels", |
| 205 | + "stylers": {"visibility": "off"}, |
| 206 | + }, |
| 207 | + { |
| 208 | + "featureType": "arterial", |
| 209 | + "elementType": "geometry", |
| 210 | + "stylers": {"color": "#fefefe"}, |
| 211 | + }, |
| 212 | + { |
| 213 | + "featureType": "arterial", |
| 214 | + "elementType": "geometry.fill", |
| 215 | + "stylers": {"color": "#fefefe"}, |
| 216 | + }, |
| 217 | + { |
| 218 | + "featureType": "poi", |
| 219 | + "elementType": "all", |
| 220 | + "stylers": {"visibility": "off"}, |
| 221 | + }, |
| 222 | + { |
| 223 | + "featureType": "green", |
| 224 | + "elementType": "all", |
| 225 | + "stylers": {"visibility": "off"}, |
| 226 | + }, |
| 227 | + { |
| 228 | + "featureType": "subway", |
| 229 | + "elementType": "all", |
| 230 | + "stylers": {"visibility": "off"}, |
| 231 | + }, |
| 232 | + { |
| 233 | + "featureType": "manmade", |
| 234 | + "elementType": "all", |
| 235 | + "stylers": {"color": "#d1d1d1"}, |
| 236 | + }, |
| 237 | + { |
| 238 | + "featureType": "local", |
| 239 | + "elementType": "all", |
| 240 | + "stylers": {"color": "#d1d1d1"}, |
| 241 | + }, |
| 242 | + { |
| 243 | + "featureType": "arterial", |
| 244 | + "elementType": "labels", |
| 245 | + "stylers": {"visibility": "off"}, |
| 246 | + }, |
| 247 | + { |
| 248 | + "featureType": "boundary", |
| 249 | + "elementType": "all", |
| 250 | + "stylers": {"color": "#fefefe"}, |
| 251 | + }, |
| 252 | + { |
| 253 | + "featureType": "building", |
| 254 | + "elementType": "all", |
| 255 | + "stylers": {"color": "#d1d1d1"}, |
| 256 | + }, |
| 257 | + { |
| 258 | + "featureType": "label", |
| 259 | + "elementType": "labels.text.fill", |
| 260 | + "stylers": {"color": "rgba(0,0,0,0)"}, |
| 261 | + }, |
| 262 | + ] |
| 263 | + }, |
| 264 | + ) |
| 265 | + .add_js_funcs( |
| 266 | + """ |
| 267 | + var lngExtent = [39.5, 40.6]; |
| 268 | + var latExtent = [115.9, 116.8]; |
| 269 | + var cellCount = [50, 50]; |
| 270 | + var cellSizeCoord = [ |
| 271 | + (lngExtent[1] - lngExtent[0]) / cellCount[0], |
| 272 | + (latExtent[1] - latExtent[0]) / cellCount[1] |
| 273 | + ]; |
| 274 | + var gapSize = 0; |
| 275 | +
|
| 276 | + function renderItem(params, api) { |
| 277 | + var lngIndex = api.value(0); |
| 278 | + var latIndex = api.value(1); |
| 279 | + var pointLeftTop = getCoord(params, api, lngIndex, latIndex); |
| 280 | + var pointRightBottom = getCoord(params, api, lngIndex + 1, latIndex + 1); |
| 281 | +
|
| 282 | + return { |
| 283 | + type: 'rect', |
| 284 | + shape: { |
| 285 | + x: pointLeftTop[0], |
| 286 | + y: pointLeftTop[1], |
| 287 | + width: pointRightBottom[0] - pointLeftTop[0], |
| 288 | + height: pointRightBottom[1] - pointLeftTop[1] |
| 289 | + }, |
| 290 | + style: api.style({ |
| 291 | + stroke: 'rgba(0,0,0,0.1)' |
| 292 | + }), |
| 293 | + styleEmphasis: api.styleEmphasis() |
| 294 | + }; |
| 295 | + } |
| 296 | +
|
| 297 | + function getCoord(params, api, lngIndex, latIndex) { |
| 298 | + var coords = params.context.coords || (params.context.coords = []); |
| 299 | + var key = lngIndex + '-' + latIndex; |
| 300 | + return coords[key] || (coords[key] = api.coord([ |
| 301 | + +(latExtent[0] + lngIndex * cellSizeCoord[0]).toFixed(6), |
| 302 | + +(lngExtent[0] + latIndex * cellSizeCoord[1]).toFixed(6) |
| 303 | + ])); |
| 304 | + } |
| 305 | + """ |
| 306 | + ) |
| 307 | + .add( |
| 308 | + series_name="", |
| 309 | + data_pair=j["data"], |
| 310 | + type_=ChartType.CUSTOM, |
| 311 | + render_item=JsCode("renderItem"), |
| 312 | + itemstyle_opts=opts.ItemStyleOpts(color="yellow"), |
| 313 | + encode={"tooltip": 2}, |
| 314 | + ) |
| 315 | + .set_global_opts( |
| 316 | + title_opts=opts.TitleOpts(title="BMap-Custom 图"), |
| 317 | + tooltip_opts=opts.TooltipOpts(is_show=True, formatter=None), |
| 318 | + visualmap_opts=opts.VisualMapOpts( |
| 319 | + is_piecewise=True, |
| 320 | + pos_top="10", |
| 321 | + pos_left="10", |
| 322 | + is_inverse=True, |
| 323 | + pieces=[ |
| 324 | + {"value": i, "color": color_list[i]} for i in range(len(color_list)) |
| 325 | + ], |
| 326 | + dimension=2, |
| 327 | + border_color="#ccc", |
| 328 | + border_width=2, |
| 329 | + background_color="#eee", |
| 330 | + ), |
| 331 | + graphic_opts=[ |
| 332 | + opts.GraphicGroup( |
| 333 | + graphic_item=opts.GraphicItem( |
| 334 | + rotation=JsCode("Math.PI / 4"), |
| 335 | + bounding="raw", |
| 336 | + right=110, |
| 337 | + bottom=110, |
| 338 | + z=100, |
| 339 | + ), |
| 340 | + children=[ |
| 341 | + opts.GraphicRect( |
| 342 | + graphic_item=opts.GraphicItem( |
| 343 | + left="center", top="center", z=100 |
| 344 | + ), |
| 345 | + graphic_shape_opts=opts.GraphicShapeOpts( |
| 346 | + width=400, height=50 |
| 347 | + ), |
| 348 | + graphic_basicstyle_opts=opts.GraphicBasicStyleOpts( |
| 349 | + fill="rgba(0,0,0,0.3)" |
| 350 | + ), |
| 351 | + ), |
| 352 | + opts.GraphicText( |
| 353 | + graphic_item=opts.GraphicItem( |
| 354 | + left="center", top="center", z=100 |
| 355 | + ), |
| 356 | + graphic_textstyle_opts=opts.GraphicTextStyleOpts( |
| 357 | + text="Made by pyecharts", |
| 358 | + font="bold 26px Microsoft YaHei", |
| 359 | + graphic_basicstyle_opts=opts.GraphicBasicStyleOpts( |
| 360 | + fill="#fff" |
| 361 | + ), |
| 362 | + ), |
| 363 | + ), |
| 364 | + ], |
| 365 | + ) |
| 366 | + ], |
| 367 | + ) |
| 368 | + ) |
| 369 | + return c |
| 370 | + |
| 371 | + |
165 | 372 | Page().add(*[fn() for fn, _ in C.charts]).render() |
0 commit comments