【发布时间】:2016-01-18 16:21:58
【问题描述】:
我在 Excel 中做过正弦计算器。
我尝试将图表插入工作表。
图表应为正弦波,Y 轴为幅度,X 轴为时间。
问题是我得到一个包含两个图表的图表:正弦,根据 Y 列,和线 - 根据 X 列。
这是我的代码:
Public oneTimeFlag As Integer
Sub calc()
Range("A3", Range("A2").End(xlDown)).Clear
Range("B2", Range("B2").End(xlDown)).Clear
Range("A2").Value = "0"
lw = Int(Range("$I$3").Value + 1)
If lw >= 4 And lw < 21000 Then
Range("A3").Select
ActiveCell.Formula = "=(2*PI()/$I$3)+A2"
Range("A3:A" & lw).FillDown
Range("B2").Select
ActiveCell.Formula = "=ROUNDDOWN(POWER(2,$G$2)/2 + SIN(($F$7*2*PI()/360) + A2)*((POWER(2,$G$2)/2) -1), 0)"
Range("B2:B" & lw).FillDown
AddOrUpdateChartSheet (lw)
Else
MsgBox "Nof points must be 4 at least and less than 21000!"
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$5" Or Target.Address = "$F$6" Or Target.Address = "$F$7" Then
Dim rng As Range
Set rng = Range(Selection.Address)
Call calc
rng.Select
End If
End Sub
Sub AddOrUpdateChartSheet(ByVal lw As Integer)
Dim chtChart As Chart
If oneTimeFlag <> 1 Then
oneTimeFlag = 1
Set chtChart = Charts.Add
Set chtChart = chtChart.Location(Where:=xlLocationAsObject, Name:="Sheet1")
With chtChart
.ChartType = xlLine
.SetSourceData (ActiveSheet.Range("A1:B" & lw))
.HasTitle = True
.ChartTitle.Text = "Sine"
With .Parent
.Name = "Sine"
End With
End With
Else
Dim objChrt As ChartObject
Dim sineChartExists As Boolean
sineChartExists = False
For Each objChrt In ActiveSheet.ChartObjects
If objChrt.Name = "Sine" Then
sineChartExists = True
End If
Next
If sineChartExists = False Then
oneTimeFlag = 0
AddOrUpdateChartSheet (lw)
Else
Set objChrt = ActiveSheet.ChartObjects("Sine")
Set chtChart = objChrt.Chart
With chtChart
.SetSourceData (ActiveSheet.Range("A1:B" & lw))
End With
End If
End If
End Sub
我得到类似的东西: Chart with sine wave
时钟频率、分频器和 DAC 分辨率是常数。
用户更改所需的频率、幅度和相位。
表格自动计算点数,计算点(时间,dac_value)并根据点数创建所需的图表。
如上所述,结果我得到带有两个图形的图表(X 轴是点数,Y 轴 - 是幅度(DAC 值))。
我需要一张图表(正弦),X 轴为时间(A 列),Y 轴为幅度(B 列)。
【问题讨论】: