|
将在图表对象上绘制的多边形控制点转换成图表内对应的数据点,我写了一个VB宏,但是转换的点与实际的多边形始终有一点差距,不明白原因,望高手赐教呀!!!
代码
Sub macwu1()
Dim sht1, sht2, sht3 As ChartArea
Dim sht As Chart
Dim oform As Shape
Dim num, i, j As Integer
Dim Points(5000, 2) As Double
Dim xst, xed, yst, yed As Double
Dim ShapeTop, ShapeLeft, ShapeWidth, ShapeHeight As Double
Dim xmin, xmax, ymin, ymax As Double
Dim xmin1, xmax1, ymin1, ymax1 As Double
Dim shxmin, shxmax, shymin, shymax As Double
Dim cleft, ctop, cheight, cwidth As Integer
'获得绘图区边界及几何大小
Sheets("数据编辑").Select
cleft = ActiveChart.PlotArea.Left
ctop = ActiveChart.PlotArea.Top
cheight = ActiveChart.PlotArea.Height
cwidth = ActiveChart.PlotArea.Width
'获得坐标最大最小值
xmin = ActiveChart.Axes(xlCategory).MinimumScale
xmax = ActiveChart.Axes(xlCategory).MaximumScale
ymin = ActiveChart.Axes(xlValue).MinimumScale
ymax = ActiveChart.Axes(xlValue).MaximumScale
'取相反数是为了和屏幕坐标系统一致
ymin1 = -ymax
ymax1 = -ymin
'获得多边形坐标
ActiveChart.Shapes("Freeform 1").Select
ct = Selection.Vertices
num = ActiveChart.Shapes("Freeform 1").ConnectionSiteCount
ShapeTop = ActiveChart.Shapes("Freeform 1").Top
ShapeLeft = ActiveChart.Shapes("Freeform 1").Left
ShapeWidth = ActiveChart.Shapes("freeform 1").Width
ShapeHeight = ActiveChart.Shapes("freeform 1").Height
shxmin = 100000000000#
shxmax = -100000000000#
shymin = 100000000000#
shymax = -100000000000#
For i = 1 To num - 1
Points(i, 1) = ct(i, 1)
If (Points(i, 1) > shxmax) Then
shxmax = Points(i, 1)
End If
If (Points(i, 1) < shxmin) Then
shxmin = Points(i, 1)
End If
Points(i, 2) = ct(i, 2)
If (Points(i, 2) > shymax) Then
shymax = Points(i, 2)
End If
If (Points(i, 2) < shymin) Then
shymin = Points(i, 2)
End If
Next i
For i = 1 To num - 1
Worksheets("数据").Cells(i, 8) = Points(i, 1)
Worksheets("数据").Cells(i, 9) = Points(i, 2)
Next i
'归一化
For i = 1 To num - 1
Points(i, 1) = (Points(i, 1) - shxmin) / (shxmax - shxmin)
Points(i, 1) = ShapeLeft - cleft + Points(i, 1) * ShapeWidth
Points(i, 1) = xmin + (Points(i, 1) - cleft) * (xmax - xmin) / cwidth
'Points(i, 1) = xmin + Points(i, 1) * (xmax - xmin) / cwidth
Points(i, 2) = (Points(i, 2) - shymin) / (shymax - shymin)
Points(i, 2) = ShapeTop + Points(i, 2) * ShapeHeight
Points(i, 2) = ymin1 + (Points(i, 2) - ctop) * (ymax1 - ymin1) / cheight
'Points(i, 2) = -60#
Next i
For i = 1 To 10000
Worksheets("数据").Cells(i, 10) = ""
Worksheets("数据").Cells(i, 11) = ""
Next i
For i = 1 To num - 1
Worksheets("数据").Cells(i, 10) = Points(i, 1)
Worksheets("数据").Cells(i, 11) = -Points(i, 2)
Next i
'未完………………
End Sub |
|