Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 编辑图表中图形的自定义颜色_Vb.net_Colors_Charts_Points - Fatal编程技术网

Vb.net 编辑图表中图形的自定义颜色

Vb.net 编辑图表中图形的自定义颜色,vb.net,colors,charts,points,Vb.net,Colors,Charts,Points,我的表格上有一个图表,并制作了一个子表格,将在表格中添加一个新的图表。这个很好用。 如果添加的图形是整个图表中的第一个图形,我希望它是红色的。如果新的图表不是第一个,我希望它是蓝色的。这是我的密码: Public Shared Sub graphHinzufügen(pfad As String) 'Daten aus Datei lesen und Graphen erstellen/formatieren DateiWiderstand.wertepaareA

我的表格上有一个图表,并制作了一个子表格,将在表格中添加一个新的图表。这个很好用。 如果添加的图形是整个图表中的第一个图形,我希望它是红色的。如果新的图表不是第一个,我希望它是蓝色的。这是我的密码:

Public Shared Sub graphHinzufügen(pfad As String)
        'Daten aus Datei lesen und Graphen erstellen/formatieren
        DateiWiderstand.wertepaareAusDateiLesen(pfad)
        Hauptseite.ChartWiderstand.Series.Add(System.IO.Path.GetFileName(pfad))
        With Hauptseite.ChartWiderstand.Series(System.IO.Path.GetFileName(pfad))
            .ChartType = DataVisualization.Charting.SeriesChartType.Spline
            For i = 0 To DateiWiderstand.wertepaarGeschwindigkeit.Count - 1
                .Points.AddXY(DateiWiderstand.wertepaarGeschwindigkeit.Item(i), DateiWiderstand.wertepaarWiderstand.Item(i))
            Next i
            If Hauptseite.ChartWiderstand.Series.Count = 1 Then 'ToDo: Funktioniert noch nicht bei erster Zeile!?
                .Color = Color.Red
            Else
                .Color = Color.Blue
            End If
        End With
    End Sub
问题是.Color=Color.Red不能正常工作。 我的调试尝试:

就在.Color事件之前,我在if案例中创建了msgboxes。我让他们给我这个系列。伯爵。这起作用了。所以我知道if情况是正确的,当它是第一个图时,程序在第一个if情况下运行。 我在其他事件处理程序中使用.Color=Color.Red,图形会根据需要切换颜色。因此,这应该是为图形着色的正确代码。 我的错在哪里?如果您需要更多的代码,请告诉我您希望使用哪些类。 谢谢大家!

编辑:以下是所有与我的问题相关的代码。欢迎提供所有意见和提示:

Public Class Hauptseite

    Private Sub Hauptseite_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Alle Reiter mit den temp-Dateien füllen
        ImportExportWiderstand.ReiterWiderstandImportieren()
    End Sub

    Private Sub ButtonWiderstandHinzufügen_Click(sender As Object, e As EventArgs) Handles ButtonWiderstandHinzufügen.Click
        'Datei im DGV hinzufügen und Graphen erstellen
        Dim widerstandDateiWähler As New OpenFileDialog
        If widerstandDateiWähler.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                DiagrammWiderstand.graphHinzufügen(widerstandDateiWähler.FileName)
                With Me.DataGridViewWiderstand
                    .Rows.Add()
                    .Rows(.RowCount - 1).Cells(0).Value = System.IO.Path.GetFileName(widerstandDateiWähler.FileName)
                    .Rows(.RowCount - 1).Cells(1).Value = widerstandDateiWähler.FileName
                End With
            Catch ex As Exception
                MsgBox("Datei kann nicht hinzugefügt werden. Wahrscheinlich ist der Dateiname schon vorhanden.")
            End Try
        End If
    End Sub

    Private Sub ButtonWiderstandEntfernen_Click(sender As Object, e As EventArgs) Handles ButtonWiderstandEntfernen.Click
        'Graphen löschen und Datei aus dem DGV entfernen
        With Me.DataGridViewWiderstand
            Try
                DiagrammWiderstand.graphEntfernen(Me.DataGridViewWiderstand.SelectedRows(0).Cells(1).Value)
                .Rows.Remove(.SelectedRows(0))
            Catch ex As Exception

            End Try
        End With
    End Sub

    Private Sub Hauptseite_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        'alle Informationen aus den Reitern in temp-Dateien schreiben
        ImportExportWiderstand.ReiterWiderstandExportieren()
    End Sub

    Private Sub DataGridViewWiderstand_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewWiderstand.RowEnter
        'bei Zeilenwechsel Farben der Graphen aktualisieren
        DiagrammWiderstand.FarbeDerGraphenAktualisieren(Me.DataGridViewWiderstand.Rows(e.RowIndex).Cells(1).Value)
    End Sub

End Class


Public Class DiagrammWiderstand

    Public Shared Sub graphHinzufügen(pfad As String)
        'Daten aus Datei lesen und Graphen erstellen/formatieren
        DateiWiderstand.wertepaareAusDateiLesen(pfad)
        Hauptseite.ChartWiderstand.Series.Add(System.IO.Path.GetFileName(pfad))
        With Hauptseite.ChartWiderstand.Series(System.IO.Path.GetFileName(pfad))
            .ChartType = DataVisualization.Charting.SeriesChartType.Spline
            For i = 0 To DateiWiderstand.wertepaarGeschwindigkeit.Count - 1
                .Points.AddXY(DateiWiderstand.wertepaarGeschwindigkeit.Item(i), DateiWiderstand.wertepaarWiderstand.Item(i))
            Next i
            If Hauptseite.ChartWiderstand.Series.Count = 1 Then 'ToDo: Funktioniert noch nicht bei erster Zeile!?
                .Color = Color.Red
            Else
                .Color = Color.Blue
            End If
        End With
    End Sub

    Public Shared Sub graphEntfernen(pfad As String)
        'spezifischen Graphen aus Diagramm entfernen
        If pfad <> Nothing Then
            Hauptseite.ChartWiderstand.Series.Remove(Hauptseite.ChartWiderstand.Series(System.IO.Path.GetFileName(pfad)))
        End If
    End Sub

    Public Shared Sub aktuellenGraphFärben(pfad As String)
        'spezifischen Graphen rot färben
        If pfad <> Nothing Then
            Hauptseite.ChartWiderstand.Series(System.IO.Path.GetFileName(pfad)).Color = Color.Red
        End If
    End Sub

    Public Shared Sub alleGraphenEntfärben()
        'alle Graphen im Diagram blau färben
        With Hauptseite.ChartWiderstand
            If .Series.Count <> 0 Then
                For i = 0 To .Series.Count - 1
                    .Series(i).Color = Color.Blue
                Next
            End If         
        End With
    End Sub

    Public Shared Sub FarbeDerGraphenAktualisieren(pfad As String)
        'Farben der Graphen im Diagramm aktualisieren (durch entfärben und färben)
        alleGraphenEntfärben()
        aktuellenGraphFärben(pfad)
    End Sub
End Class

通过重新组织代码,我成功地解决了我的问题。通过在每次添加、删除或输入一行时调用自写更新方法,图形的颜色会如我所希望的那样发生变化。以下是相关代码:

#Region "Reiter Widerstand"

    Private Sub ButtonWiderstandHinzufügen_Click(sender As Object, e As EventArgs) Handles ButtonWiderstandHinzufügen.Click
        Dim widerstandDateiWähler As New OpenFileDialog
        If widerstandDateiWähler.ShowDialog = Windows.Forms.DialogResult.OK Then
            CChartWiderstand.graphHinzufügen(widerstandDateiWähler.FileName)
            CDGVWiderstand.zeileHinzufügen(widerstandDateiWähler.FileName)
            CChartWiderstand.FarbeDerGraphenAktualisieren(Me.DataGridViewWiderstand.SelectedRows(0).Cells(1).Value)
        End If
    End Sub

    Private Sub ButtonWiderstandEntfernen_Click(sender As Object, e As EventArgs) Handles ButtonWiderstandEntfernen.Click
        With Me.DataGridViewWiderstand
            If .SelectedRows.Count > 0 Then
                CChartWiderstand.graphEntfernen(.SelectedRows(0).Cells(1).Value)
                CDGVWiderstand.zeileEntfernen(.SelectedRows(0).Cells(1).Value)
                If .SelectedRows.Count > 0 Then
                    CChartWiderstand.FarbeDerGraphenAktualisieren(.SelectedRows(0).Cells(1).Value)
                End If
            End If
        End With
    End Sub

    Private Sub DataGridViewWiderstand_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewWiderstand.RowEnter
        CChartWiderstand.FarbeDerGraphenAktualisieren(Me.DataGridViewWiderstand.Rows(e.RowIndex).Cells(1).Value)
    End Sub

#End Region
这是:

 Public Shared Sub aktuellenGraphFärben(pfad As String)
        If prüfenObGraphExistiert(pfad) Then
            Hauptseite.ChartWiderstand.Series(System.IO.Path.GetFileName(pfad)).Color = Color.Red
        End If
    End Sub

    Public Shared Sub alleGraphenEntfärben()
        With Hauptseite.ChartWiderstand
            If .Series.Count <> 0 Then
                For i = 0 To .Series.Count - 1
                    .Series(i).Color = Color.Blue
                Next
            End If
        End With
    End Sub

    Public Shared Sub FarbeDerGraphenAktualisieren(pfad As String)
        alleGraphenEntfärben()
        aktuellenGraphFärben(pfad)
    End Sub

它是什么类型的图表?你试过前景色吗?最后,您是否尝试过在指定颜色后强制它重新绘制?例如,Chart1.Refresh?没有像Chart.Series.Forecolor或Chart.Refresh这样的方法。因此,我无法尝试您的建议。Chart.Forecolor存在,但对我不起作用,因为它可以绘制所有图形,而不仅仅是需要的单个图形。有一种叫做Chart.update的方法,但它对我都没有帮助。它是什么类型的图表?如果是烛台,试试后二次色。你有没有试着强迫图表重新绘制自己?这是一个折线图。你可以在代码中找到它,它是一条样条曲线。所以反二次色不起作用。如何强制图表重新绘制?图表。重绘不存在。图表。更新甚至表单。更新不起作用。