ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] Ãþ¦ü²¾°Ê«ü¤Þ½uªº®ÄªG

¦^´_ 2# ®ß®ß¤l
·s¼Wª«¥óÃþ§O¼Ò²Õ¡A©R¦W EventClassModule
  1. Private WithEvents myChartClass As Chart
  2. Private myVLine As Object
  3. Private myTarget As Range

  4. Public Sub InitializeChart(objChart As Object, rngTarget As Range)
  5.     Set myChartClass = objChart
  6.     Set myTarget = rngTarget
  7.    
  8.     On Error Resume Next
  9.     Set myVLine = myChartClass.Shapes("vline")
  10.     On Error GoTo 0
  11.    
  12.     If myVLine Is Nothing Then
  13.         With myChartClass.PlotArea
  14.             Set myVLine = myChartClass.Shapes.AddLine(.InsideLeft, .InsideTop, .InsideLeft, .InsideTop + .InsideHeight)
  15.             myVLine.Name = "vline"
  16.         End With
  17.     End If
  18. End Sub

  19. Private Sub myChartClass_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
  20.     Dim pt_x As Double, interval As Double, indx As Long
  21.     Dim arValues
  22.    
  23.     pt_x = 75 * x / ActiveWindow.Zoom
  24.     interval = myChartClass.PlotArea.InsideWidth / UBound(myChartClass.SeriesCollection(1).XValues)
  25.     indx = Application.RoundUp((pt_x - myChartClass.PlotArea.InsideLeft) / interval, 0)
  26.     indx = Application.Min(Application.Max(1, indx), UBound(myChartClass.SeriesCollection(1).XValues))
  27.         
  28.     With myChartClass.SeriesCollection
  29.         If .Count = 0 Then Exit Sub
  30.         For i = 1 To .Count
  31.             With .Item(i)
  32.                 .ApplyDataLabels Type:=xlDataLabelsShowNone
  33.                 .Points(indx).ApplyDataLabels Type:=xlDataLabelsShowValue
  34.                 .Points(indx).DataLabel.Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
  35.                 arValues = .Values
  36.                 If i <= myTarget.Cells.Count Then myTarget.Cells(i + 1).Value = arValues(indx)
  37.             End With
  38.         Next
  39.         arValues = .Item(1).XValues
  40.         myTarget.Cells(1).Value = arValues(indx)
  41.     End With
  42.    
  43.     With myChartClass
  44.         If .Axes(xlCategory).AxisBetweenCategories Then
  45.             myVLine.Left = .PlotArea.InsideLeft + (indx - 0.5) * interval
  46.         Else
  47.             myVLine.Left = .PlotArea.InsideLeft + (indx - 1) * interval
  48.         End If
  49.     End With
  50. End Sub
½Æ»s¥N½X
¤@¯ë¼Ò²Õ¡G
  1. Dim myChart As EventClassModule

  2. Sub TriggerChartVLine()
  3.     With Sheets(1)
  4.         Set myChart = New EventClassModule
  5.         myChart.InitializeChart .ChartObjects(1).Chart, .Range("U15:U18")
  6.     End With
  7. End Sub

  8. Sub Auto_Open()
  9.     TriggerChartVLine
  10. End Sub
½Æ»s¥N½X
Index.zip (93.23 KB)
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

¦^´_ 9# ®ß®ß¤l
1. ¯àªþÀɮ׬ݬݤñ¸û¦n¡A©Î¬O¸Õ¸Õ§ï¦¨³o¼Ë
  1. Private Sub myChartClass_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
  2.     Dim pt_x As Double, interval As Double, indx As Long
  3.     Dim arValues, targetX
  4.     Dim isAxisBetween As Boolean, dataCount As Long, diffX, xOffset
  5.    
  6.     pt_x = 75 * x / ActiveWindow.Zoom
  7.    
  8.     With myChartClass
  9.         If .SeriesCollection.Count = 0 Then Exit Sub
  10.         isAxisBetween = .Axes(xlCategory).AxisBetweenCategories '®y¼Ð¶b¦ì¸m ¨è«×¶¡:True ¨è«×¤W:False
  11.         arValues = .SeriesCollection(1).XValues '¤é´Á¸ê®Æ
  12.         dataCount = UBound(arValues)    '¸ê®Æ¼Æ¥Ø
  13.         With .Axes(xlCategory)
  14.             diffX = .MaximumScale - .MinimumScale
  15.             xOffset = IIf(isAxisBetween, 0.5 * myChartClass.PlotArea.InsideWidth / (diffX + 1), 0)
  16.             If pt_x < myChartClass.PlotArea.InsideLeft + xOffset Then
  17.                 indx = 1
  18.             Else
  19.                 targetX = .MinimumScale + diffX * (pt_x - myChartClass.PlotArea.InsideLeft - xOffset) / (myChartClass.PlotArea.InsideWidth - 2 * xOffset)
  20.                 indx = Application.Match(targetX, arValues, 1)
  21.                 If indx < UBound(arValues) Then
  22.                     If Abs(targetX - arValues(indx + 1)) < Abs(targetX - arValues(indx)) Then indx = indx + 1   '­×¥¿³Ìªñ¸ê®ÆÂI
  23.                 End If
  24.             End If
  25.             
  26.         End With
  27.         myVLine.Left = .PlotArea.InsideLeft + xOffset + (.PlotArea.InsideWidth - 2 * xOffset) * (arValues(indx) - arValues(1)) / CDbl(diffX)
  28.         myTarget.Cells(1).Value = arValues(indx)
  29.         For i = 1 To .SeriesCollection.Count
  30.             With .SeriesCollection(i)
  31.                 .ApplyDataLabels Type:=xlDataLabelsShowNone
  32.                 .Points(indx).ApplyDataLabels Type:=xlDataLabelsShowValue
  33.                 .Points(indx).DataLabel.Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
  34.                 arValues = .Values
  35.                 If i < myTarget.Cells.Count Then myTarget.Cells(i + 1).Value = arValues(indx)
  36.             End With
  37.         Next
  38.     End With
  39. End Sub
½Æ»s¥N½X
2. MouseMove ¬O Chart(¹Ïªí) ¤º«Øªº¨Æ¥ó¡A·í·Æ¹««ü¼Ð¦b¹Ïªí¤Wªº¦ì¸m§ïÅܮɷ|¦Û°ÊIJµo¦¹¨Æ¥ó¡C
    ¨Æ¥óªº°Ñ¼Æ·|¦Û¤v¶Ç¤J¡A§A¥u­nª¾¹D¶Ç¤JªºªF¦è¨ì©³¬O¥Nªí¤°»ò¡C
    ¦U°Ñ¼Æ»¡©ú¥i¥H¦Û¤vF1¬d MouseMove±oª¾¡C
    Button  : ¨Æ¥óµo¥Í®É¡A·Æ¹««öÁ䪬ºA
    Shift : ¨Æ¥óµo¥Í®É SHIFT¡BCTRL ©M ALT Á䪺ª¬ºA
    x : ·Æ¹««ü¼Ð¦b¹Ïªíª«¥ó¤u§@°Ï®y¼Ð¤¤ªº X ®y¼Ð¡C
    y : ·Æ¹««ü¼Ð¦b¹Ïªíª«¥ó¤u§@°Ï®y¼Ð¤¤ªº Y ®y¼Ð¡C
3. ¬O¦]¬°®y¼Ð¶b®æ¦¡¤¤¡A®y¼Ð¶b¦ì¸m¤À¬°¨è«×¤W©M¨è«×»P¨è«×¶¡¬Û¶Z¨âºØ¡A·|¼vÅT¸ê®ÆÂI¦ì¸m¡C
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

¥»©«³Ì«á¥Ñ stillfish00 ©ó 2015-12-24 15:11 ½s¿è

¦^´_ 11# ®ß®ß¤l
1.  indx = Application.Match(targetX, arValue, 1)
     ³o¥y arValue §ï¦¨ arValues , µ§»~¡C

3. §Ú¤]¬O¤Wºô¬dªº¡A§A¦b¬d Chart ªº InsideLeft ©Î InsideWidth ³o¨ÇÄݩʮɳ£·|§i¶D§A¥¦¬O¥H"ÂI"¬°³æ¦ì¡A¤@­ÓÂI©w¸q¬O1/72­^¦T (¤]¦³¤H©w¸q¬°1/72.27­^¦T)¡C

¦Ó¶Ç¤J°Ñ¼Æªº  x , y ³æ¦ì¬O pixel(¹³¯À)¡F®Ú¾Úwindows¤ºÅã¥Ü¾¹ DPI(dot per inch) ³]©w¥i¨M©w¨C­^¦T¦h¤Ö¹³¯À¡A¤@¯ë¹w³]¬° normal size 100% (=96 DPI)¡Aªí¥Ü¨C­^¦T 96 ¹³¯À¡C

©Ò¥H­n§â x ¹³¯ÀÂনÂI­n­¼¥H 72/96=0.75
ÁÙ­n¦Ò¼{ zoom , ©Ò¥HÅܦ¨  0.75*x/(ActiveWindow.zoom/100) = 75*x/ActiveWindow.zoom
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

¦^´_ 11# ®ß®ß¤l
­×§ï myVLine.Left ±q¸Ó¸ê®ÆÂIªº left Äݩʨú±o¡A¦p¤U
§A»¡ªºµLªkºë·Çªº¹ï¤W®y¼Ð¡A¨º¬O§A¸ê®Æ¤Ó±K¶°¡A¹Ïªí¥»¨­¤é´Á®y¼Ð¶b´N·|³o¼Ë(¨C­Ó¤ë¤Ñ¼Æ¬O¤£¦Pªº¡A¥¦«o¦P¶¡¹j)
  1. Private Sub myChartClass_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
  2.     Dim pt_x As Double, interval As Double, indx As Long
  3.     Dim arValues, targetX, diffX
  4.     Dim isAxisBetween As Boolean, xOffset
  5.    
  6.     pt_x = 75 * x / ActiveWindow.Zoom
  7.    
  8.     With myChartClass
  9.         If .SeriesCollection.Count = 0 Then Exit Sub
  10.         isAxisBetween = .Axes(xlCategory).AxisBetweenCategories '®y¼Ð¶b¦ì¸m ¨è«×¶¡:True ¨è«×¤W:False
  11.         arValues = .SeriesCollection(1).XValues '¤é´Á¸ê®Æ
  12.         diffX = .Axes(xlCategory).MaximumScale - .Axes(xlCategory).MinimumScale
  13.         xOffset = IIf(isAxisBetween, 0.5 * .PlotArea.InsideWidth / (diffX + 1), 0)
  14.         
  15.         If pt_x < .PlotArea.InsideLeft + xOffset Then
  16.             indx = 1
  17.         Else
  18.             targetX = .Axes(xlCategory).MinimumScale + diffX * (pt_x - .PlotArea.InsideLeft - xOffset) / (.PlotArea.InsideWidth - 2 * xOffset)
  19.             indx = Application.Match(targetX, arValues, 1)
  20.             If indx < UBound(arValues) Then If Abs(targetX - arValues(indx + 1)) < Abs(targetX - arValues(indx)) Then indx = indx + 1  '­×¥¿³Ìªñ¸ê®ÆÂI
  21.         End If
  22.             
  23.         myVLine.Left = .SeriesCollection(1).Points(indx).Left
  24.         myTarget.Cells(1).Value = arValues(indx)
  25.         For i = 1 To .SeriesCollection.Count
  26.             With .SeriesCollection(i)
  27.                 .ApplyDataLabels Type:=xlDataLabelsShowNone
  28.                 .Points(indx).ApplyDataLabels Type:=xlDataLabelsShowValue
  29.                 .Points(indx).DataLabel.Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
  30.                 arValues = .Values
  31.                 If i < myTarget.Cells.Count Then myTarget.Cells(i + 1).Value = arValues(indx)
  32.             End With
  33.         Next
  34.     End With
  35. End Sub
½Æ»s¥N½X
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

        ÀR«ä¦Û¦b : ¨C¤ÑµL©Ò¨Æ¨Æ¡A¬O¤H¥Íªº®ø¶OªÌ¡A¿n·¥¡B¦³¥Î¤~¬O¤H¥Íªº³Ð³yªÌ¡C
ªð¦^¦Cªí ¤W¤@¥DÃD