占比图监听尺寸大小
This commit is contained in:
parent
57f8815e85
commit
a86e73f14b
@ -539,7 +539,7 @@ export const DEFAULT_GRAPHIC_TEXT_STYLE: ChartGraphicTextStyle = {
|
|||||||
positionX:'50',
|
positionX:'50',
|
||||||
positionY:'50',
|
positionY:'50',
|
||||||
fontSize: 18,
|
fontSize: 18,
|
||||||
fill: '#000000',
|
fill: '#ffffff',
|
||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
textAlign: 'center',
|
textAlign: 'center',
|
||||||
offsetX:0,
|
offsetX:0,
|
||||||
|
@ -22,20 +22,30 @@ import {
|
|||||||
*/
|
*/
|
||||||
class ChartWrapper {
|
class ChartWrapper {
|
||||||
chartInstance: Chart
|
chartInstance: Chart
|
||||||
|
private resizeObserver?: ResizeObserver
|
||||||
|
|
||||||
constructor(chartInstance: Chart) {
|
constructor(chartInstance: Chart, resizeObserver?: ResizeObserver) {
|
||||||
this.chartInstance = chartInstance
|
this.chartInstance = chartInstance
|
||||||
|
this.resizeObserver = resizeObserver
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.chartInstance?.destroy?.()
|
this.chartInstance?.destroy?.()
|
||||||
|
|
||||||
|
// 清理 ResizeObserver
|
||||||
|
if (this.resizeObserver) {
|
||||||
|
this.resizeObserver.disconnect()
|
||||||
|
this.resizeObserver = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
this.chartInstance?.render?.()
|
this.chartInstance?.render?.()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class G2Pie extends G2ChartView {
|
export class G2Pie extends G2ChartView {
|
||||||
|
private resizeObserver: ResizeObserver | null = null
|
||||||
|
|
||||||
axis: AxisType[] = PIE_AXIS_TYPE
|
axis: AxisType[] = PIE_AXIS_TYPE
|
||||||
// properties = PIE_EDITOR_PROPERTY
|
// properties = PIE_EDITOR_PROPERTY
|
||||||
@ -44,7 +54,6 @@ export class G2Pie extends G2ChartView {
|
|||||||
'border-style',
|
'border-style',
|
||||||
'basic-style-selector',
|
'basic-style-selector',
|
||||||
'title-selector',
|
'title-selector',
|
||||||
'label-selector',
|
|
||||||
'tooltip-selector',
|
'tooltip-selector',
|
||||||
'jump-set',
|
'jump-set',
|
||||||
'linkage',
|
'linkage',
|
||||||
@ -53,7 +62,7 @@ export class G2Pie extends G2ChartView {
|
|||||||
propertyInner: EditorPropertyInner = {
|
propertyInner: EditorPropertyInner = {
|
||||||
...PIE_EDITOR_PROPERTY_INNER,
|
...PIE_EDITOR_PROPERTY_INNER,
|
||||||
'basic-style-selector': ['colors', 'alpha', 'radius','innerRadius', 'seriesColor','proportionBackgroundColor'],
|
'basic-style-selector': ['colors', 'alpha', 'radius','innerRadius', 'seriesColor','proportionBackgroundColor'],
|
||||||
'tooltip-selector': [...PIE_EDITOR_PROPERTY_INNER['tooltip-selector'], 'carousel'],
|
'tooltip-selector': [...PIE_EDITOR_PROPERTY_INNER['tooltip-selector']],
|
||||||
|
|
||||||
}
|
}
|
||||||
axisConfig = PIE_AXIS_CONFIG
|
axisConfig = PIE_AXIS_CONFIG
|
||||||
@ -64,13 +73,12 @@ export class G2Pie extends G2ChartView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async drawChart({ container, chart }: { container: HTMLElement; chart: ChartObj }) {
|
async drawChart({ container, chart }: { container: HTMLElement; chart: ChartObj }) {
|
||||||
|
const element = document.getElementById(container)
|
||||||
const customAttr = parseJson(chart.customAttr)
|
const customAttr = parseJson(chart.customAttr)
|
||||||
const customStyle = parseJson(chart.customStyle)
|
const customStyle = parseJson(chart.customStyle)
|
||||||
|
console.log(chart,96564)
|
||||||
const innerRadius = customAttr.basicStyle.innerRadius ?? 60
|
const innerRadius = customAttr.basicStyle.innerRadius ?? 60
|
||||||
const data = cloneDeep(chart.data?.data || [])
|
const data = cloneDeep(chart.data?.data || [])
|
||||||
const { radius = 0.75, alpha = 1, colors = [] } = customAttr.basicStyle
|
|
||||||
|
|
||||||
const colorList = customAttr.basicStyle.colors.map(i =>
|
const colorList = customAttr.basicStyle.colors.map(i =>
|
||||||
hexColorToRGBA(i, customAttr.basicStyle.alpha)
|
hexColorToRGBA(i, customAttr.basicStyle.alpha)
|
||||||
)
|
)
|
||||||
@ -85,21 +93,43 @@ export class G2Pie extends G2ChartView {
|
|||||||
})
|
})
|
||||||
|
|
||||||
g2.data(data)
|
g2.data(data)
|
||||||
|
|
||||||
g2.legend(false)
|
g2.legend(false)
|
||||||
g2.tooltip({
|
if(customAttr.tooltip.show == true){
|
||||||
showMarkers: false,
|
g2.tooltip({
|
||||||
showContent: (datum) => {
|
showMarkers: false,
|
||||||
return !datum.some(item => item?.name === '其他');
|
enterable:true,
|
||||||
},
|
showContent: (datum) => {
|
||||||
})
|
console.log(datum)
|
||||||
|
return !datum.some(item => item?.name === '其他');
|
||||||
|
},
|
||||||
|
|
||||||
|
domStyles: {
|
||||||
|
'g2-tooltip': {
|
||||||
|
backgroundColor: customAttr.tooltip.backgroundColor, // 背景色
|
||||||
|
color: customAttr.tooltip.color, // 文字颜色
|
||||||
|
boxShadow: '0 2px 2px rgba(0,0,0,0.15)', // 阴影
|
||||||
|
// borderRadius: '4px', // 圆角
|
||||||
|
// padding: '8px 12px' // 内边距
|
||||||
|
},
|
||||||
|
'g2-tooltip-title': {
|
||||||
|
fontSize: customAttr.tooltip.fontSize + 'px',
|
||||||
|
// fontWeight: 'bold',
|
||||||
|
// marginBottom: '4px'
|
||||||
|
},
|
||||||
|
'g2-tooltip-list-item': {
|
||||||
|
fontSize: customAttr.tooltip.fontSize + 'px'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}else{
|
||||||
|
g2.tooltip(false)
|
||||||
|
}
|
||||||
g2.facet('rect', {
|
g2.facet('rect', {
|
||||||
fields: ['name'],
|
fields: ['name'],
|
||||||
padding: 20,
|
padding: 20,
|
||||||
showTitle: false,
|
showTitle: false,
|
||||||
eachView: (view, facet) => {
|
eachView: (view, facet) => {
|
||||||
const data:any = facet.data
|
const data:any = facet.data
|
||||||
console.log(customAttr,999)
|
|
||||||
let color
|
let color
|
||||||
customAttr.basicStyle.seriesColor.forEach((item) => {
|
customAttr.basicStyle.seriesColor.forEach((item) => {
|
||||||
if(data.name == item.name){
|
if(data.name == item.name){
|
||||||
@ -133,7 +163,6 @@ export class G2Pie extends G2ChartView {
|
|||||||
const positionarr:any = []
|
const positionarr:any = []
|
||||||
positionarr.push(item.positionX + '%')
|
positionarr.push(item.positionX + '%')
|
||||||
positionarr.push(item.positionY + '%')
|
positionarr.push(item.positionY + '%')
|
||||||
console.log(positionarr)
|
|
||||||
if(item.type == '名称' && item.show == true){
|
if(item.type == '名称' && item.show == true){
|
||||||
view.annotation().text({
|
view.annotation().text({
|
||||||
position: positionarr,
|
position: positionarr,
|
||||||
@ -164,36 +193,17 @@ export class G2Pie extends G2ChartView {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// view.annotation().text({
|
|
||||||
// position: [ '50%', '50%' ],
|
|
||||||
// content: data[0].name,
|
|
||||||
// style: {
|
|
||||||
// fontSize: 12,
|
|
||||||
// fill: '#8c8c8c',
|
|
||||||
// fontWeight: 300,
|
|
||||||
// textBaseline: 'bottom',
|
|
||||||
// textAlign: 'center'
|
|
||||||
// },
|
|
||||||
// offsetY: -12,
|
|
||||||
// })
|
|
||||||
|
|
||||||
// view.annotation().text({
|
|
||||||
// position: ['50%', '50%'],
|
|
||||||
// content: data[0].value,
|
|
||||||
// style: {
|
|
||||||
// fontSize: 18,
|
|
||||||
// fill: '#fff',
|
|
||||||
// fontWeight: 500,
|
|
||||||
// textAlign: 'center'
|
|
||||||
// },
|
|
||||||
// offsetY: 10,
|
|
||||||
// })
|
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
g2.render()
|
g2.render()
|
||||||
return new ChartWrapper(g2)
|
this.resizeObserver = new ResizeObserver(() => {
|
||||||
|
g2.changeSize(element.offsetWidth, element.offsetHeight)
|
||||||
|
// g2.render()
|
||||||
|
})
|
||||||
|
|
||||||
|
this.resizeObserver.observe(element)
|
||||||
|
|
||||||
|
return new ChartWrapper(g2, this.resizeObserver)
|
||||||
}
|
}
|
||||||
public setupSeriesColor(chart: ChartObj, data?: any[]): ChartBasicStyle['seriesColor'] {
|
public setupSeriesColor(chart: ChartObj, data?: any[]): ChartBasicStyle['seriesColor'] {
|
||||||
return setUpSingleDimensionSeriesColor(chart, data)
|
return setUpSingleDimensionSeriesColor(chart, data)
|
||||||
|
Loading…
Reference in New Issue
Block a user